Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 当我的条件为false时,当它变为true时,为什么循环不停止_Python_Function_Python 3.x_While Loop_Statements - Fatal编程技术网

Python 当我的条件为false时,当它变为true时,为什么循环不停止

Python 当我的条件为false时,当它变为true时,为什么循环不停止,python,function,python-3.x,while-loop,statements,Python,Function,Python 3.x,While Loop,Statements,这是我的密码 在Python3.4中,它只是一直运行循环,直到玩家死亡时系统退出。这是我一直在玩的rpg游戏的一部分 非完整代码请注意,如果您想查看完整代码,请仅询问显示的作战功能 def combat(h, a, d, x): global attack global health global defence global xp death = False while death == False: eab = random.r

这是我的密码 在Python3.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函数进行比较,后者永远不会起作用。你需要做的是用death=True替换它

这将修复您的代码。

death==True。你不是在给死亡赋值,你只是在比较它们。