[Tutor] Tutor Digest, Vol 111, Issue 68

Jim Mooney cybervigilante at gmail.com
Mon May 20 19:22:22 CEST 2013


>  > 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

This gave rise to an interesting behavior, though. I ran it with
random outside the loop and indeed got the same number. Except the
number Changed right toward the end to one other number. I got a lot
of 19s, then a 17. The third time I got a lot of 23s, then a lot of
70s. This was consistent. Then I scrolled the results more slowly and
realized the program ran over 25,000 times and the anomalous numbers
were toward the end.

So why did it run about 25,000 times and why the change in number?

Here's the prog so you can see the loopvar count does increment so it
Should stop at 100.

import random

Count = 0
Random_Number = random.randint(1, 100)

Count = 0

while Count <= 100:

    print (Random_Number)
    Count = Count + 1

I found the cause. If I just print Count it only goes to 100. If I
print Random_Number it goes to 25K. Is this consistent or a strange
flaw of Pyscripter?


Jim


More information about the Tutor mailing list