Oliver
Oliver

Reputation: 19

Python lists and sorting randomly generated numbers

I have this

import random 

a = random.randint(1,100) 
b = random.randint(1,100) 
c = random.randint(1,100) 
d = random.randint(1,100) 
e = random.randint(1,100) 

print "The numbers are a:",a,"b:",b,"c:",c,"d:",d,"and e:",e 
print "The numbers in ascending order:" 
list1=[a,b,c,d,e]
list1.sort(key=int)
print a
print b
print c
print d
print e`

But I can't get it to understand that I want to print the new values and not the old ones for a-e

Upvotes: 2

Views: 1338

Answers (6)

Diego M
Diego M

Reputation: 11

By creating and sorting list1 you change the contents of the list, not the actual variables a, b, c, d and e. If you wanted python to print the "new values", you would have to print list1.

import random 

a = random.randint(1,100) 
b = random.randint(1,100) 
c = random.randint(1,100) 
d = random.randint(1,100) 
e = random.randint(1,100) 

print "The numbers are a:",a,"b:",b,"c:",c,"d:",d,"and e:",e 
print "The numbers in ascending order:" 
list1=[a,b,c,d,e]
list1.sort(key=int)

for num in list1:
    print num

Upvotes: 1

YaleCheung
YaleCheung

Reputation: 620

import random 

a = random.randint(1,100) 
b = random.randint(1,100) 
c = random.randint(1,100) 
d = random.randint(1,100) 
e = random.randint(1,100) 

print "The numbers are a:",a,"b:",b,"c:",c,"d:",d,"and e:",e 
print "The numbers in ascending order:" 
list1=[a,b,c,d,e]
list1.sort(key=int)
for item in list1:
    print(str(item) + '\n')

Something wrong with your conception. Yeah, a,b,c,d,e are created by random.randint(). When you sort the list1, a,b,c,d,e won't be changed by the changes of list1. Because in list1, only values copied from a,b,c,d,e inside, not a,b,c,d,e themselves. So you cannot change the values of a,b,c,d,e by changing the list1.

Upvotes: 1

SamAko
SamAko

Reputation: 3615

This works

import random 

a = random.randint(1,100) 
b = random.randint(1,100) 
c = random.randint(1,100) 
d = random.randint(1,100) 
e = random.randint(1,100) 

print "The numbers are a:",a,"b:",b,"c:",c,"d:",d,"and e:",e 
print "The numbers in ascending order:" 
list1=[a,b,c,d,e]
list1.sort(key=int)

for item in list1:
    print item

Upvotes: 1

DJG
DJG

Reputation: 6543

When you sort the list, the original variables that you assigned do not get changed, only the contents of the list do.

You can reassign the variables from the sorted list:

list1=[a,b,c,d,e]
list1.sort(key=int)
a, b, c, d, e = list1

print a
print b
print c
print d
print e

Upvotes: 2

tbraun89
tbraun89

Reputation: 2234

You put the numbers of the variables into the list list1. If you sort the list you don't change the values of a to e.

If you want to print them sorted itterate over the list and print all the elements, then they are sorted.

for current in list1:
    print current

Upvotes: 1

TerryA
TerryA

Reputation: 59974

Iterate through list1 and just print each value:

for number in list1:
    print number

Upvotes: 3

Related Questions