为什么我在代码中收到“UnboundLocalError”?

问题描述:

我已经修改了报价,以解决语法错误。现在,我收到了错误,这是一个:为什么我在代码中收到“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

您需要将所有换算为报价('")。另外你需要检查你的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

“变量赋值之前引用”只是意味着你正在使用一个尚不存在的变量。在你的代码中,问题是这样的:

totalPints += pints[counter] 

这是第一次出现totalPints。请记住,“+ =”建设是完全等同于

totalPints = totalPints + pints[counter] 

,它的右侧出现Python是反对。为了解决这个问题,你进入循环之前

totalPints = 0 

初始化变量。

那么这是一个容易解决。 您只需在添加内容之前分配变量。

totalPins = 0 

totalPins = "" 

之前进入循环应该做的伎俩。