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

Mitya Sirenef msirenef at lightbird.net
Mon May 20 19:13:43 CEST 2013


On 05/20/2013 12:55 PM, Thomas Murphy wrote:
> Mitya,
> Why is it best in this situation to use range() rather than a while
> loop? Curious about best practices for the various iterating
> functions. Thanks!
>
>
>> 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

It's a simpler and more idiomatic approach, as you can
see, you don't need to initialize the counter and you don't
need to manually increment it.  -m


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



More information about the Tutor mailing list