In Python.....

Associate
Joined
29 Jan 2008
Posts
217
I am trying to learn Python and was looking over for loops and while loops.

Should the following be using a while loop as it is waiting for user input?

When the 5 numbers have been selected, how do I print what has been randomly selected at the end?

import random
low = ['1', '2', '3', '4', '5']
high = ['10', '20', '30', '40', '50']

print "Please Enter Low or High"
print "Press 1 for Low"
print "Press 2 for High"
print

count = 0

for number in range (5):
choice = raw_input ("Low or High: ")
if choice == '1':
choice = random.choice(low)
count = count + number
print choice
elif choice == '2':
choice = random.choice(high)
count = count + number
print choice
 
It's no particular part geuben, it's just knowing what to use and when to use it. It will eventually sink in.

I tried your code for printing out the 5 random numbers but it gives me this:

Press 1 for Low
Press 2 for High

2
10
2
10
2
10
2
20
1
2
['40', '50', '30', '30', '5']

Where does it get those numbers from when it should be ['10', '10', '10', '20', '2']?
 
Last edited:
It appears to be adding random numbers to chosen.

chosen = []

for count in range( 0, 5 ):
while True:
choice = raw_input()
if choice == "1":
chosen.append( random.choice( low ) )
print random.choice( low )
break
elif choice == "2":
chosen.append( random.choice( high ) )
print random.choice( high )
break
else:
print "invalid input"

print chosen
 
Back
Top Bottom