2013-06-01 77 views
1
g=0 
def smooth(self, a, b): 
    k=0 
    c = self.name[a] 
    d = self.name[b] 
    e,f=c,d 
    while(e.get_p()!=f.get_p() and e.get_p()!=None and f.get_p()!=None): 
     k+=1 
     e=e.get_p() 
     f=f.get_p() 
    if(e.get_p==None and f.get_p()!=None): 
     global g 
     g+=1 
     d=d.get_p() 
     return self.smooth(a,d.name) 
    return(k,g) 

忽略調用的函數,但是在if語句它沒有更新克價值,給了一個錯誤全局名稱「G」是不是在用value.Please幫助全局變量給錯誤

調用定義
+1

是所有的代碼?你確定這不在'class'內嗎? – slezica

回答

3

在此代碼:

g=0 
def smooth(self, a, b): 

smooth看起來像一個類的實例方法,以及g因此看起來像一個類變量,而不是一個全球性的,所以global關鍵字不會活像ķ。請嘗試將其改爲MyClass.g(其中「MyClass」是您班級的實際名稱)或__class__.g

+1

+1用於檢測顯然缺少密鑰行的問題。好眼睛 – slezica