How can i stop this Infinite While Loop - Python

Peter Otten __peter__ at web.de
Wed Oct 30 13:19:48 EDT 2019


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


More information about the Python-list mailing list

AltStyle によって変換されたページ (->オリジナル) /