0

所以我决定写一个程序,看看在《部落冲突》中,2级黑暗灵药钻出比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 语句,但它正在更新日期和小时变量。

4

2 回答 2

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 语句终止最近的封闭循环。

于 2013-07-13T01:37:50.183 回答
1

这个怎么样?

while second < first:
    # exactly the same code
于 2014-04-22T15:22:15.497 回答