2015-04-03 180 views
-1

我試圖解決在checkio(房子密碼)的問題 ..my代碼如下什麼是

def checkio(data): 
    if len(data)>9: 
     for i in data: 
      if str.isdigit(i)==True: 
       global counternumber 
       counternumber=counternumber+1 
      if str.isupper(i)==True: 
       global counterupper 
       counterupper=counterupper+1 
      if str.islower(i)==True: 
       global counterlower 
       counterlower=counterlower+1 
    if (counternumber>1 & counterupper>1 & counterlower>1): 
     return True 
else: 
    return False 

此功能,而試圖實現如下因素錯誤「分配之前引用局部變量」彈出

NameError:全局名稱「counterupper」沒有定義

聲明爲全局變量錯誤之前會彈出

UnboundLocalError: local variable 'counterupper' referenced before assignment, 

這些錯誤意味着什麼以及如何解決它們?

請解釋清楚,因爲我是新來的編程..

+0

你並沒有真正搜索,是嗎? http://stackoverflow.com/search?q=local+variable+referenced+before+assignment – 2015-04-03 12:36:22

回答

1

可以在短寫:

def checkio(data): 
    return (len(data) > 9 and 
     any(ch.isdigit() for ch in data) and 
     any(ch.isupper() for ch in data) and 
     any(ch.islower() for ch in data))