简体   繁体   中英

How do I exit this while loop?

I'm having trouble with exiting the following while loop. This is a simple program that prints hello if random value is greater than 5. The program runs fine once but when I try to run 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

Your while loop might increment the variable count by 0, 1, 2 or 3. This might result in count going from a value below 20 to a value over 20.

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

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

After these operations, count's value would be 18 + 3 = 21, which is not 20. Therefore, the condition value == 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 program logic inside the while loop.

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

Here's one way to get exactly 20 lines.

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

Note there is still a lot of repetition in this code. Storing your a,b,c variables in a list instead of as separate variables would allow the code to be simplified further

The "break" condition might fail if two or more values of the variables a , b , and c are greater than 5. In that case the count will be incremented more than once and count will end up > 20, and the loop can never terminate. You should change:

  if count == 20 :

to

  if count >= 20:

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

if count >= 20:

to feel safe.

If your goal is to stop counting when count is >= 20 , then you should use that condition for your while loop and you won't need to break at all, as you only break at the end of the loop anyways.

The new while statement would look like

while count < 20:
    # increment count

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

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 have to check the value of count after incrementing it every time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM