簡體   English   中英

Python NameError,變量“未定義”

[英]Python NameError, variable 'not defined'

它返回的錯誤是:

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 之外定義變量“lives”,然后在要引用該全局變量的任何函數中定義“global life”。 當您在函數中並為變量賦值時,它假定它在局部作用域中。 使用“全局生命”告訴該函數將全局范圍視為生命的參考。

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()

您沒有在main()中將lives聲明為全局的,因此它是該函數的局部變量。

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()

現在這工作正常。 有關全局變量與局部變量的更多信息,您可以閱讀: http ://www.python-course.eu/global_vs_local_variables.php

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM