Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.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 赋值前引用的变量_Python_Variables - Fatal编程技术网

Python 赋值前引用的变量

Python 赋值前引用的变量,python,variables,Python,Variables,我经历了几次这样的事情,但都没有发现任何错误,所以我可能有点不知所措。我也为这将对你的眼睛造成的伤害道歉,这是我编程的第一年,可能犯了多个礼仪错误 print('Befor we begin, when you are given options, I ask you to type your input as show, failure to do so will break the program and you will lose all of your progress') def te

我经历了几次这样的事情,但都没有发现任何错误,所以我可能有点不知所措。我也为这将对你的眼睛造成的伤害道歉,这是我编程的第一年,可能犯了多个礼仪错误

print('Befor we begin, when you are given options, I ask you to type your input as show, failure to do so will break the program and you will lose all of your progress')
def test():
    print('it has worked!')
def stats():
    global attack
    global health
    global mashealth
    global agility
    if race in ('human','Human'):
        maxatk=4
        maxagi=4
        attack = lambda:random.randint(2,maxatk)
        maxhealth = 20
        health=20
        agility = random.randint(maxagi,10)


    elif race in ('Orc','orc'):
        attack = random.randint(3,maxATK)
        maxhealth = 25
        agility = random.radint(maxAGI,10)


def main():
    while True:
        print('What would you like to do')
        print('Rest   Shop   Fight')
        answer=input('-')
        if answer in ('Rest','rest'):
            health=maxhealth
            continue

def character():
    global race
    global name
    global gender
    print('What would you like the name of your character to be?')
    name=input('-')
    print()
    print('What race will your character be?')
    print('Human   Orc   Elf')
    while True:
        race = input('-')
        if race in ('human','Human','Orc','orc','Elf','elf'):
            break
        else:
            print('Not a valid response, try again')
            continue
    print()
    print('What gender is your character')
    gender=input('-')
    print()

def goblin():
    goblinatk=1
    goblinhealth=100
    while True:
        print('You have encountered a goblin, what will you do?')
        do=input('-')
        if do == 'attack':
            damage=attack()
            goblinhealth=goblinhealth-damage
            print('You did',damage,'damage to the goblin')
            print('The goblin has',goblinhealth,'hp')
        goblinatk=lambda:random.randint(3,10)
        health=health-goblinatk
        print('the goblin did',goblinatk,'to you')
        print('you have',health,'hp')
        if goblinhealth <0:
            print('The goblin has died')
            break
        if health <0:
            print('you have died')
            break






character()
stats()
goblin()
test()
错误就在这里

Traceback (most recent call last):
  File "H:\16CoFrega\Code\Python\GAME.py", line 255, in <module>
    goblin()
  File "H:\16CoFrega\Code\Python\GAME.py", line 238, in goblin
    health=health-goblinatk
UnboundLocalError: local variable 'health' referenced before assignment
您需要将健康状况指定为全局变量,否则它将被视为局部变量,因为您在函数中为其赋值。范例-

def goblin():
    global health
    goblinatk=1
    goblinhealth=100
看看这个函数定义。你的函数不知道什么是健康,所以它不允许你从健康中下标

所以,不知何故,功能必须认识到什么是健康。两种最常见的方法:

第一种方法,将health声明为全局变量。现在,健康可以得到全球的认可。但这不是最好的方法,因为处理全局变量很难而且容易出错,而且您已经处理了太多全局变量。所以,我不推荐它。我建议你用方法2替换所有的全局变量。为了理解我为什么这么说

第二种方法,推荐的方法,是将health变量作为函数的参数传递,最后从函数返回

像这样:

def goblin(health):
    ............
    health=health-goblinatk
    ............
    return health
如果你已经归还了一些东西,不要担心。使用python,可以将多个变量作为元组返回

返回语句:

return a,b,c
a,b,c = func()
通话声明:

return a,b,c
a,b,c = func()
希望这有助于: