一旦满足条件,如何停止循环程序

时间:2013-07-13 01:30:38

标签: python python-3.2

所以我决定编写一个程序,看看2级黑暗长生不老药师在Clash of Clans中产生超过1级的水平需要多长时间。升级需要两天时间,因此1级将获得两天的黑暗灵药开始。

我的问题是,如何使循环停止(或如何使用不同的循环)停止一次>第一λ

hour = 0
first = 960
second = 0
days = 0
while True:
    hour = hour + 4
    if hour - 24 >= 0:
        hour = hour - 24
        days = days + 1
    else:
        hour = hour
    first = first + 80
    second = second + 120
    if first >= second:
        print("After", days, "days and", hour, "hours, a level 1 produces", first, "dark elixir, while a level 2 produces", second, "dark elixir!")
    else:
        print("After", days, ",", hour, "hours, a level 2 produces more dark elixir than a level one!")

现在正在发生的是它正在打印else语句,但它正在更新日期和小时变量。

2 个答案:

答案 0 :(得分:1)

if first >= second:
    print("After", days, "days and", hour, "hours, a level 1 produces", first, "dark elixir, while a level 2 produces", second, "dark elixir!")
else:
    print("After", days, ",", hour, "hours, a level 2 produces more dark elixir than a level one!")
    break

break语句终止最近的封闭循环。

答案 1 :(得分:1)

这个怎么样?

while second < first:
    # exactly the same code