2

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

1
  • 3
    (Side note: You don't need key=int here, the numbers already are integers. key=int would only make sense for mixed-type lists like ["1", 2, "3"]) Commented Sep 30, 2013 at 8:46

6 Answers 6

3

Iterate through list1 and just print each value:

for number in list1:
    print number
Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

1

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

Comments

1

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

Comments

1
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.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.