My Python code generates a random number between 0 and 35 and stores that number as ‘random’. I am looking to compare ‘random’ against several ranges of numbers to determine which of three groups it falls into and assign another value to ‘winning’ based on which group ‘random’ is in. The groups are: 0-16, 16-34, and 34-36. ‘winning’ always returns 2 though. Here’s what I’ve tried so far.
import random #start python random number generator
random = random.randrange(36) #calls a random number between 0-35 and stores value
print random #test
for random in range(0, 16):
winning = 0
for random in range(16, 34):
winning = 1
for random in range(34, 36):
winning = 45
print winning #test
Thank you in advance for any help that you can give! This is my first time programming with Python.
You should use
ifinstead offor.foris a loop keyword.Actually this is not a great use of
rangesince it will generate a full list of all numbers and then check each number to see if it equalsrandom. It would be more efficient to do some simple comparisons: