简体   繁体   中英

How end the loop if the total number exceed the certain number?

I wanted to end the loop when the total of rabbits is over 500 but it doesn't seem to work right now, is there any other way to end the loop if it overs a certain number?

    for i in range(20):
        timePasses()
        displayEndOfTheMonthStatus()
        totalRabbits
        if totalRabbits == 500:
            break

print("out of cages")

if __name__ == "__main__":
    main()

You could try using a while loop (eg

while(totalRabbits < 501):
    do things

Alternatively, check your condition statement. You are breaking on if totalRabbits is exactly equal to 500? Is this condition ever met? Is it possible for totalRabbits to skip from say 499 to 501 directly?

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