【问题标题】:Break out of Python loop, without break跳出 Python 循环,不中断
【发布时间】:2015-08-19 23:23:01
【问题描述】:

在下面的代码中,为什么打架设置为False时不停止循环?

我知道它不会停止循环,因为当战斗设置为 False 时它不会进入战利品部分。这是整个while循环:

while fighting:
    cls()
    print("The enemy has", opponent.HP, "HP!")
    input()
    if int(opponent.HP) <= 0:
        print("Yep yep")
        winner = True
        fighting = False
    elif int(ownedCreatures[activeCreature].HP) <= 0:
        winner = False
        fighting = False
    showFight(opponent, activeCreature)
    allowed = ["a", "i", "r"]
    choice = input(">>")

    while not choice in allowed:
        choice = input("Try again please >>")

    if choice.lower() == "a":
        if previousTurn == "Not defined":
            num = random.randint(1, ownedCreatures[activeCreature].support + opponent.support)
            if num <= ownedCreatures[activeCreature].support:
                attacker = "player"
                previousTurn = "player"
            else:
                attacker = "opponent"
                previousTurn = "opponent"
        else:
            if previousTurn == "player":
                attacker = "opponent"
                previousTurn = "opponent"
            else:
                attacker = "player"
                previousTurn = "player"

        attack(attacker, activeCreature, opponent)

    #if choice.lower() == "i":

    if choice.lower() == "r":
        num = random.randint(1, ownedCreatures[activeCreature].support + opponent.support)
        if num <= ownedCreatures[activeCreature].support:
            cls()
            print("-------------------------------------------")
            print("You succesfully escaped this horrible fight!")
            print("-------------------------------------------\n")
            input("Press Enter to continue... >> ")
            winner = "Not defined"
            fighting = False
        else:
            cls()
            print("-------------------------------------------")
            print("Think you can run that easily?")
            print("-------------------------------------------\n")
            input("Press Enter to continue... >> ")

#After the fight
if winner == True:
    cls()
    loot()
elif winner == False:
    cls()
    print("-------------------------------------------")
    print("You have lost the fight!")
    print("You lost 50 Serra!")
    serra = serra - 50
    if serra < 0:
        serra = 0
    print("-------------------------------------------\n")
    input("Press Enter to continue... >> ")

【问题讨论】:

  • 你怎么知道它不会停止?这就是你在 while 循环中所做的一切吗?
  • 你能发个MCVE吗?
  • 请打印opponent.HP的值
  • @RicardoFrederiks 你能打印这些值吗:opponent.HPownedCreatures[activeCreature].HPownedCreatures[activeCreature].support + opponent.support
  • ownedCreatures[activeCreature].support,对手.support

标签: python loops while-loop break


【解决方案1】:

您在循环中的三个位置将fighting 设置为False,并且它们都带有if 条件:

  1. int(opponent.HP) &lt;= 0
  2. int(ownedCreatures[activeCreature].HP) &lt;= 0
  3. num &lt;= ownedCreatures[activeCreature].support

第一个和第二个条件在循环中是不变的,所以如果它们开始Falsefighting的变化将永远无法访问。

第三个:num 是一个大于1 的随机数,因此如果ownedCreatures[activeCreature].support0,则条件将永远无法访问。

打印条件值以检查它们是否满足。

【讨论】:

  • OP 说“为什么当战斗设置为 False 时它不停止循环”所以我认为他确定它设置为 False
  • 我确实确定 HP 低于或等于 0。我用 prints 和 input() 进行了检查。
  • 当我放一个“休息”时它确实有效,但我不想使用休息。
  • @RicardoFrederiks 你应该发布整个代码,你已经向我们展示了没有elseif
  • @bigOTHER 如果这就是你的意思,战利品部分没有其他内容了吗?
猜你喜欢
  • 2020-05-21
  • 1970-01-01
  • 2021-08-31
  • 1970-01-01
  • 1970-01-01
  • 2021-08-29
  • 1970-01-01
  • 2013-01-13
  • 1970-01-01
相关资源
最近更新 更多