2015-06-11 57 views
-1

我試圖用一個函數中的全局變量,儘管全球範圍內聲明的變量,並初始化它的價值,我得到以下錯誤:UnboundLocalError在Python,儘管聲明全局變量來

Traceback (most recent call last): 
File "test.py", line 11, in <module> 
    main() 
File "test.py", line 8, in main 
    func_check() 
File "test.py", line 5, in func_check 
    value += 45 
UnboundLocalError: local variable 'value' referenced before assignment 

以下是代碼片段:

value = 0 
def func_check(): 
    value += 45 

def main(): 
    func_check() 

if __name__ == "__main__": 
    main() 

回答

1

修改func_check

def func_check(): 
    global value 
    value += 45 
+2

'全球'是高度氣餒。最好使用參數 –