Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/356.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 有人能给我解释一下为什么';健康';调用updateGame()函数时不更新var_Python - Fatal编程技术网

Python 有人能给我解释一下为什么';健康';调用updateGame()函数时不更新var

Python 有人能给我解释一下为什么';健康';调用updateGame()函数时不更新var,python,Python,我一直在网上寻找一些答案,但是我仍然不清楚为什么在调用getDamage()函数时不更新“health”变量 我正在开始学习python health = 200.0 maxHealth = 200 healthDashes = 20 dashConvert = int(maxHealth/healthDashes) currentDashes = int(health/dashConvert) remainingHealth = healthD

我一直在网上寻找一些答案,但是我仍然不清楚为什么在调用getDamage()函数时不更新“health”变量

我正在开始学习python

health = 200.0 
maxHealth = 200
healthDashes = 20
dashConvert = int(maxHealth/healthDashes)           
currentDashes = int(health/dashConvert)             
remainingHealth = healthDashes - currentDashes 
healthDisplay = '-' * currentDashes       
remainingDisplay = ' ' * remainingHealth
percent = str(int((health/maxHealth)*100)) + "%"
gameOver = False


def updateGame():
    print(chr(27) + "[2J")
    print (30 * '-')
    print("")
    print("    |" + healthDisplay + remainingDisplay + "|")
    print("         health " + percent)                      
    print ("")
    print (30 * '-')
    print("")

def getDamage():
    global health
    health = 10



while gameOver == False:
    answer = raw_input("> ").lower()
    if answer == "help":
        print("")
        print(" you can use the following commands: h, i, q, d")
        print("")
    elif answer == "q":
        print("\n")
        print("Game Over")
        print("")
        break
    elif answer == "h":
        updateGame()
    elif answer == "d":
        getDamage()
    else:
        print(""" not a valid command, see "help" """)
我能做些什么来正确更新“health”变量,并在下次调用getDamage()函数时显示降低的运行状况

基本上,我试图实现的是一个基于文本的游戏,在while循环中运行,并具有不同的函数来更新主函数(updateGame),该函数显示有关玩家状态的相关信息,如健康状况、库存项目

我试图实现的逻辑是: 让getDamage()减少health变量,然后用updateGame()显示新更改的变量


非常感谢

在updateGame函数中,您从未引用全局变量health。如果希望在函数内部更改运行状况,则需要访问它

这意味着您应该有如下内容:

def updateGame():
全球卫生
健康=更新的健康
...

然后,每次调用函数时,它都应该更改。调用
getDamage()
时,健康将更改为
10
,但
healthDisplay
remainingDisplay
percent
是在脚本的第一个部分设置的,不会每次更改
healt
全局变量。因此,每次调用
updateGame()
函数时,必须在其中更改它们。而且我猜
health=10
必须改为
health-=10

health = 200.0 
maxHealth = 200
healthDashes = 20

gameOver = False


def updateGame():
    dashConvert = int(maxHealth/healthDashes)           
    currentDashes = int(health/dashConvert)             
    remainingHealth = healthDashes - currentDashes 
    healthDisplay = '-' * currentDashes       
    remainingDisplay = ' ' * remainingHealth
    percent = str(int((health/maxHealth)*100)) + "%"
    print(chr(27) + "[2J")
    print (30 * '-')
    print("")
    print("    |" + healthDisplay + remainingDisplay + "|")
    print("         health " + percent)                      
    print ("")
    print (30 * '-')
    print("")

def getDamage():
    global health
    health -= 10



while gameOver == False:
    answer = input("> ").lower()
    if answer == "help":
        print("")
        print(" you can use the following commands: h, i, q, d")
        print("")
    elif answer == "q":
        print("\n")
        print("Game Over")
        print("")
        break
    elif answer == "h":
        updateGame()
    elif answer == "d":
        getDamage()
    else:
        print(""" not a valid command, see "help" """)

更准确一点。问题是什么。将
运行状况
声明为
全局
时,可以在函数中修改全局变量