简体   繁体   English

如果总数超过一定数量,如何结束循环?

[英]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?我想在兔子总数超过 500 只时结束循环,但现在似乎不起作用,如果超过某个数量,还有其他方法可以结束循环吗?

    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循环(例如

while(totalRabbits < 501):
    do things

Alternatively, check your condition statement.或者,检查您的条件语句。 You are breaking on if totalRabbits is exactly equal to 500?如果totalRabbits正好等于 500,您就在突破吗? Is this condition ever met?这个条件曾经满足吗? Is it possible for totalRabbits to skip from say 499 to 501 directly? totalRabbits是否可以直接从 499 跳到 501?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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