How to exit a while loop?

I'm having trouble exiting the while loop. This is a simple program that prints hello if the random value is greater than 5. The program runs fine once, but when I try to start it again, it goes into an infinite loop.

  from random import * seed() a = randint(0,10) b = randint(0,10) c = randint(0,10) count = 0 while True: if a > 5 : print ("aHello") count = count + 1 else : a = randint(0,10) if b > 5 : print ("bHello") count = count + 1 else : b = randint(0,10) if c > 5 : print ("cHello") count = count + 1 else : c = randint(0,10) if count == 20 : count = 0 break count = 0 
+5
source share
6 answers

Does the following code help?

 while True: if a > 5 : print ("aHello") count = count + 1 if count == 20 : break else : a = randint(0,10) if b > 5 : print ("bHello") count = count + 1 if count == 20 : break else : b = randint(0,10) if c > 5 : print ("cHello") count = count + 1 if count == 20 : break else : c = randint(0,10) 

You should check the value of the counter after increasing it each time.

+1
source

Your while loop can increment the variable counter by 0, 1, 2, or 3. This can cause the count to go from a value below 20 to a value greater than 20.

For example, if the count value is 18, and the following happens:

 a > 5, count += 1 b > 5, count += 1 c > 5, count += 1 

After these operations, the value of the account will be 18 + 3 = 21, which is not equal to 20. Therefore, the value of the condition == 20 will never be met.

To fix the error, you can either replace the line

 if count == 20 

with

 if count >= 20 

or just change your programming logic inside a while loop.

+8
source

since you increment count 2 or 3 times in one iteration, it may skip the check count == 20

Here is one way to get exactly 20 rows.

 from random import seed, randint seed() a = randint(0,10) b = randint(0,10) c = randint(0,10) count = iter(range(20)) while True: try: if a > 5: next(count) print ("aHello") else: a = randint(0,10) if b > 5: next(count) print ("bHello") else: b = randint(0,10) if c > 5: next(count) print ("cHello") else: c = randint(0,10) except StopIteration: break 

Notice that there is still a lot of repetition in this code. Storing variables a, b, c in list instead of individual variables would simplify the code further

+1
source

The break condition may fail if two or more values โ€‹โ€‹of the variables a , b and c greater than 5. In this case, the count will increase more than once and the count will be completed up> 20, and the cycle can never end. You must change:

  if count == 20 : 

to

  if count >= 20: 
+1
source

At the end of the iteration, count may be greater than 20 due to several increments. So I would update the last if statement:

 if count >= 20: 

to feel safe.

+1
source

If your goal is to stop counting when count - >= 20 , then you should use this condition for your while loop, and you don't need to break at all, since you only break at the end of the loop anyway.

The new while statement will look like

 while count < 20: # increment count 

and then outside the while loop, you can reset to count 0 if you want to use it again for something else.

+1
source

Source: https://habr.com/ru/post/1237546/


All Articles