脱离Python循环,没有b

2024-09-23 06:35:12 发布

您现在位置:Python中文网/ 问答频道 /正文

在下面的代码中,为什么当战斗设置为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... >> ")

Tags: falsesupportinputifclsplayerprintchoice
1条回答
网友
1楼 · 发布于 2024-09-23 06:35:12

在循环中有三个地方将fighting设置为False,所有这些地方都带有一个if条件:

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

第一个和第二个条件在循环中是恒定的,因此如果它们启动False,那么{}的更改将永远无法访问。在

第三个:num是一个大于1的随机数,所以如果ownedCreatures[activeCreature].support是{},那么这个条件将永远无法访问。在

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

相关问题 更多 >