2016-11-23 49 views
-4

每當我嘗試將Python中的變量更改爲全局變量時,在代碼實際運行之前,我會收到一條錯誤消息。儘管出現這些錯誤消息,代碼依然運行正常。這是我得到的:來自警告模塊的全局變量警告

Warning (from warnings module): 
    File "N:\Documents\Computer Science\Sample CAB 2\Estimate\Task 2 Estimate.py", line 202 
    global loop 
SyntaxWarning: name 'loop' is assigned to before global declaration 

任何人都可以幫忙嗎?

回答

2

該警告是不言自明:

name 'loop' is assigned to before global declaration

移動第一分配上述global減速。

a = 1 
global a 

SyntaxWarning: name 'a' is assigned to before global declaration 
    global a 

相比:

global a 
a = 1 
0

你將不得不使用裏面的程序之前,定義一個全局變量。爲了供您參考,我在此向您展示演示程序。

g = 100 #it is global 
def func(l): 
    print(l) #local value of x 
    global g 
    print(g) #global value of x 
    g = 120 
func(10) 
print(g) 

輸出將是,

10 
100 
120