2012-07-30 33 views
-2

我已經修改了報價,以解決語法錯誤。現在,我收到了錯誤,這是一個:爲什麼我在代碼中收到「UnboundLocalError」?

Traceback (most recent call last): 
    File "C:\Users\Alex\Desktop\Programming Concepts\Labs\Chapter 11\Lab 8.py", line 78, in <module> 
    main() 
    File "C:\Users\Alex\Desktop\Programming Concepts\Labs\Chapter 11\Lab 8.py", line 18, in main 
    totalPints = getTotal(pints) 
    File "C:\Users\Alex\Desktop\Programming Concepts\Labs\Chapter 11\Lab 8.py", line 42, in getTotal 
    totalPints += pints[counter] 
    UnboundLocalError: local variable 'totalPints' referenced before assignment 

這是到目前爲止我的代碼:

# Lab 8-3 Blood Drive 

# The main function 
def main(): 
    endProgram = 'no' 
    print 
    while endProgram == 'no': 
     print 
     # Declare variables 
     pints = [0] * 7 

     # Function calls 
     pints = getPints(pints) 
     totalPints = getTotal(pints) 
     averagePints = getAverage(totalPints) 
     highPints = getHigh(pints) 
     lowPints = getLow(pints) 
     displayInfo(averagePints, highPints, lowPints) 

     endProgram = input('Do you want to end program? (Enter no or yes): ') 
     while not (endProgram == 'yes' or endProgram == 'no'): 
      print('Please enter a yes or no') 
      endProgram = input('Do you want to end program? (Enter no or yes): ') 

# The getPints function 
def getPints(pints): 
    counter = 0 
    while counter < 7: 
     numEntered = input('Enter pints collected: ') 
     pints[counter] = int(numEntered) 
     counter += 1 
    return pints 

# The getTotal function 
def getTotal(pints): 
    counter = 0 
    while counter < 7: 
     totalPints += pints[counter] 
     counter += 1 
    return totalPints 

# The getAverage function 
def getAverage(totalPints): 
    averagePints = float(totalPints)/7 
    return averagePints 

# The getHigh function 
def getHigh(pints): 
    highPints = pints[0] 
    counter = 1 
    while counter < 7: 
     if pints[counter] > highPints: 
      highPints = pints[counter] 
     counter += 1 
    return highPints 

# The getLow function 
def getLow(): 
    lowPints = pints[0] 
    counter = 1 
    while counter < 7: 
     if pints[counter] < lowPints:\ 
      lowPints = pints[counter] 
     counter += 1 
    return lowPints 

# The displayInfo function 
def displayInfo(averagePints, highPints, lowPints): 
    print('The average number of pints donated is ',averagePints) 
    print('The highest pints donated is ', highPints) 
    print('The lowest number of pints donated is ', lowPints) 

# Calls main 
main() 

如果任何人都可以將此代碼複製並粘貼到自己的Python和幫助解決它,我會不勝感激!

+5

你寫的用Microsoft Word或其他一些文字處理你的代碼? – jamieb 2012-07-30 03:35:45

回答

3

您需要將所有換算爲報價('")。另外你需要檢查你的getPints函數內部縮進:

# The getPints function 
def getPints(pints): 
counter = 0 
while counter < 7: 
    numEntered = input(‘Enter pints collected: ‘) 
    pints[counter] = int(numEntered) 
    counter += 1 
return pints 

縮進一個多層次的一切,就像你在main功能做了功能定義後:

# The getPints function 
def getPints(pints): 
    counter = 0 
    while counter < 7: 
     numEntered = input(‘Enter pints collected: ‘) 
     pints[counter] = int(numEntered) 
     counter += 1 
    return pints 
+0

我必須添加 - 函數getLow沒有用任何參數定義,而他的函數調用 lowPints = getLow(pints) 他應該開始使用內置函數的python,因爲遍歷列表的速度非常緩慢http:// docs。 python.org/library/functions.html,雖然它不會影響性能,在這種情況下 – user1462442 2012-07-30 03:39:51

+0

@ user1462442是的,但我認爲這可能是一個不同的問題。我更新了OP面臨的實際問題的標題 – 2012-07-30 03:48:09

0

「變量賦值之前引用」只是意味着你正在使用一個尚不存在的變量。在你的代碼中,問題是這樣的:

totalPints += pints[counter] 

這是第一次出現totalPints。請記住,「+ =」建設是完全等同於

totalPints = totalPints + pints[counter] 

,它的右側出現Python是反對。爲了解決這個問題,你進入循環之前

totalPints = 0 

初始化變量。

0

那麼這是一個容易解決。 您只需在添加內容之前分配變量。

totalPins = 0 

totalPins = "" 

之前進入循環應該做的伎倆。

相關問題