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
 
Try this:

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

How would you print/store the five randomly selected numbers at the end?

Code:
chosen = []

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

# outside of for loop
print chosen
 
Which part are you having trouble with?

EDIT: Also just noticed you don't actually need the "While True" loop. I copied this from so old code. You can remove that line and the breaks
EDIT2: I knew it was there for some reason. The while loop handles the case when someone just presses enter instead of inputting a number. keep it in there!
 
Last edited:
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:
Post your code.

at a guess. you have something like this

print random.choice( high )
chosen.append( random.choice( high ) )
 
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
 
Code:
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

Use code tags when posting code. Spacing in Python is very important :)
 
Code:
Use code tags when posting code.  Spacing in Python is very important :)[/QUOTE]

I tried code tags and it removed my spacing!

[CODE]chosen.append( random.choice( high ) )
print random.choice( high )

This appends a random number to the chosen list. Then prints another random number. Each call to random.choice() results in another random number.

Change it to this:

Code:
random_number = random.choice( high)
chosen.append( random_number )
print random_number

and the same for low.
 
Back
Top Bottom