[Tutor] Random Number Game: Returns always the same number - why?

Mitya Sirenef msirenef at lightbird.net
Mon May 20 18:43:52 CEST 2013


On 05/20/2013 12:31 PM, Rafael Knuth wrote:
> Hello,
 >
 > I wrote a simple program, and I was expecting that I would get 100 
different random numbers. Instead, I am getting 100 times exactly the 
same random number. Can anyone advise how I should alter my program?
 >
 > Thank you!
 >
 > All the best,
 >
 > Rafael
 > PS. I am using Python 3.3.0
 >
 > print ("""
 >
 > This game will return 100 random numbers between 1 and 100.
 >
 > """)
 >
 > import random
 >
 > Count = 0
 >
 > Random_Number = random.randint(1, 100)
 >
 > while Count <= 100:
 > print (Random_Number)
 >
 > Count = Count + 1
 >
 >
 >


There are a few issues here:
* variable names should be lower case
* for this case it's best to use for loop with range()
* you calculate random number only once, outside of loop

Try something like:

for count in range(100):
print random.randint(1, 100)


-m

-- 
Lark's Tongue Guide to Python: http://lightbird.net/larks/

“So many books, so little time.”
― Frank Zappa



More information about the Tutor mailing list