git.net

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

How can i stop this Infinite While Loop - Python


ferzan saglam wrote:

> On Wednesday, October 30, 2019 at 2:19:32 PM UTC, Matheus Saraiva wrote:

>> rounds = 0
>> while rounds <= 10:
       ...

> Thanks, it Works superbly.
> To get the limit of 10 i wanted, i had to make a slight change:
> while rounds <= 9 .

That's the (in)famous "off by one" bug ;)

To stay consistent with Python's range() and slices it's best to always use 
"half-open" intervals which include the lower, but not the upper bound. 

In your case you would keep the start value of 0 and the limit of 10, but 
change the comparison:

rounds = 0
while rounds < 10:
   ...
   rounds += 1