Python名称错误,变量“未定义”

Python名称错误,变量“未定义”,python,variables,python-3.5,Python,Variables,Python 3.5,它返回的错误是: NameError: name 'lives' is not defined 我知道代码并没有尽可能的高效,这是我的第一个项目之一,但是无论我尝试做什么,都会出现这个错误,我已经尝试为它制作一个全局代码,但是没有帮助。我真的很感谢你的帮助,谢谢 import random import time def main(): global guess,rand_num win = False rand_num = 45 lives = 10 while lives &g

它返回的错误是:

NameError: name 'lives' is not defined
我知道代码并没有尽可能的高效,这是我的第一个项目之一,但是无论我尝试做什么,都会出现这个错误,我已经尝试为它制作一个全局代码,但是没有帮助。我真的很感谢你的帮助,谢谢

import random
import time

def main():
 global guess,rand_num
 win = False
 rand_num = 45
 lives = 10
 while lives > 0 and win == False:
     guess = int(input("Guess a number!"))
     compare()
 print("Well done!")
 time.sleep(3)

def compare():
 global lives,win
 if guess == rand_num:
     print("You guessed correct!")
     win = True
 elif guess > rand_num:
     print ("Guess lower!")
     lives = lives - 1
 else:
     print ("Guess higher!")
     lives = lives - 1

def repeat():
 replay = input("would you like to play again? Y/N")
 if replay == "Y":
     print("enjoy!")
     main()
 elif replay == "N":
     "Goodbye then, hope you enjoyed!"
     time.sleep(3)
     os._exit
 else:
     print("please enter Y or N")
     repeat()

main()
repeat()
编辑:将全局生命放入main中会返回错误:

UnboundLocalError: local variable 'lives' referenced before assignment
您没有在main中声明lifes是全局的,所以它是该函数的局部

def main():
    global guess, rand_num, lives
    ...

当您在函数内声明它时,它们仅在该函数作用域中可用,所以在函数和代码外声明全局变量可以正常工作

import random
import time

guess = None
random_num = None
lives = 3
win = False


def main():
 global guess,rand_num
 win = False
 rand_num = 45
 lives = 10
 while lives > 0 and win == False:
     guess = int(input("Guess a number!"))
     compare()
 print("Well done!")
 time.sleep(3)

def compare():
 global lives,win
 if guess == rand_num:
     print("You guessed correct!")
     win = True
 elif guess > rand_num:
     print ("Guess lower!")
     lives = lives - 1
 else:
     print ("Guess higher!")
     lives = lives - 1

def repeat():
 replay = input("would you like to play again? Y/N")
 if replay == "Y":
     print("enjoy!")
     main()
 elif replay == "N":
     "Goodbye then, hope you enjoyed!"
     time.sleep(3)
     os._exit
 else:
     print("please enter Y or N")
     repeat()

main()
repeat()

现在这个很好用。有关gloval变量与局部变量的更多信息,请阅读:

您需要在函数main之外定义变量生命,然后在任何要引用该全局变量的函数中都可以称为全局生命。当您在函数中为变量赋值时,它会假定它在局部范围内。使用全局生命告诉函数将全局范围作为生命的参考

import random
import time

lives = 10
win = False
guess = 0
rand_num = 45

def main():
    global guess, rand_num, lives, win
    win = False
    rand_num = 45
    lives = 10
    while lives > 0 and win == False:
        guess = int(input("Guess a number!"))
        compare()
    print("Well done!")
    time.sleep(3)

def compare():
    global guess, rand_num, lives, win
    if guess == rand_num:
        print("You guessed correct!")
        win = True
    elif guess > rand_num:
        print ("Guess lower!")
        lives = lives - 1
    else:
        print ("Guess higher!")
        lives = lives - 1

def repeat():
    replay = input("would you like to play again? Y/N")
    if replay == "Y":
        print("enjoy!")
        main()
    elif replay == "N":
        "Goodbye then, hope you enjoyed!"
        time.sleep(3)
        os._exit
    else:
        print("please enter Y or N")
        repeat()

main()
repeat()

错误更改为:UnboundLocalError:Assignment之前引用的局部变量“lives”更改了我的答案,请查看@chepner的答案。全局声明应该是主要功能。如果您这样做,您的代码工作正常。一般建议:避免使用全局变量。编写函数,而不是过程。Python程序员通常使用4个空格进行缩进。我在compare和main中都使用了全局4,lifes,win,rand_num和guess,这似乎很有效,但我仍然不确定这两个函数是否都需要4个空格,或者在哪里需要哪一个空格@Zondowhy在避免全局变量方面做得好@Eli Korvigo?p、 这是我唯一能弄明白怎么做的方法,正如我在问题中所说,我对编码还不熟悉;如果在该函数中设置变量,则应声明该变量为全局变量。要使x=4影响全局命名空间,需要将x声明为全局。要打印x,您不需要。