【问题标题】:When my conditional is false in when it turns true why wont the loop stop当我的条件为假时,当它变为真时,为什么循环不会停止
【发布时间】:2016-01-09 10:53:40
【问题描述】:

这是我的代码 在 python 3.4 中,它只是一直运行循环,直到玩家死亡时系统退出这是我一直在制作的 rpg 游戏的一部分。

不是完整的代码请注意如果您想查看完整的代码请询问仅显示的战斗功能

def combat(h, a, d, x):
    global attack
    global health
    global defence
    global xp
    death = False
    while death == False:
        eab = random.randint(1, 10)
        yab = random.randint(1, 10)
        cattack = 0
        cdefence = 0
        cattack = attack
        cdefence = defence
        cattack = cattack + yab
        a = a + eab
        d = d - cattack
        print("you strike for " + str(cattack) + " damage")
        cattack = cattack - d
        if cattack > 0:
            h = h - cattack
        if d > 0:
            print("the enemy has " + str(d) + "defence left")
        if h > 0:
            print("the enemy has " + str(h) + "Health left")
        else :
            print("the enemy dies")
            death == True
            xp = xp + x
        print("they strike back for " + str(a))
        cdefence = cdefence - a
        a = a - cdefence
        if a > 0:
            health = health - a
        if cdefence > 0:
            print("you have " + str(cdefence) + " defence left!")
        if health > 0:
            print("you have " + str(health) + " health left")
        else :
            sys.exit("YOU DIED A HORRIBLE DEATH")

【问题讨论】:

  • death == True。您没有将True 分配给death,您只是在比较它们。

标签: python function python-3.x while-loop statements


【解决方案1】:

你的问题在于这一行:death == True

这会将变量“死亡”与True 函数进行比较,该函数永远不会起作用。您需要做的是将其替换为 death = True

这应该可以修复您的代码。

【讨论】:

    猜你喜欢
    • 2020-07-21
    • 1970-01-01
    • 2017-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-08
    • 1970-01-01
    • 2022-11-28
    相关资源
    最近更新 更多