2016-06-21 53 views
0

它返回的錯誤是:Python的NameError,變「沒有定義」

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 
+0

看看@ chepner的答案。 'global'聲明應該在'main'函數中。如果你這樣做,你的代碼工作正常。 – zondo

+0

一般建議:避免全局變量。編寫函數,而不是程序。 Python程序員通常使用4個空格進行縮進。 –

+0

我把所有4,生命,贏得,rand_num和猜測在全球比較()和主要(),似乎已經奏效,但我仍然不確定是否所有4都需要在兩個,或哪些是需要在哪裏。 @Zondo – Kaos

回答

1

你需要在函數main之外定義變量「lives」,然後在你想要引用那個全局變量的地方定義你所說的「global lives」。當你在一個函數中併爲一個變量賦值時,它假定它在本地範圍內。使用「全球生活」告訴我們,作爲生命參照的全球範圍的功能。

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

你沒有申報lives是全球性的內部main(),所以它是局部的功能。

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

當你在函數中聲明它們時,它們只在該函數範圍內可用,所以在函數和代碼之外聲明全局變量將工作正常。

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和局部變量的更多信息:http://www.python-course.eu/global_vs_local_variables.php

+0

錯誤更改爲:UnboundLocalError:分配前引用的本地變量'lives' – Kaos

+0

更改了我的答案,請仔細閱讀 – Teemo

相關問題