2012-07-30 4 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() 

사람이 복사 자신의 파이썬에이 코드를 붙여 나는 것입니다 그것을 해결하는 데 도움이 될 수있는 경우 훌륭한!

+5

Microsoft Word 또는 다른 워드 프로세서를 사용하여 코드를 작성하고 있습니까? – jamieb

답변

3

모든 을 따옴표 (' 또는 ")로 변경해야합니다.

# 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

funtion이 호출되는 동안 getLow가 매개 변수로 정의하지 않는 함수를 추가해야합니다. lowPints ​​= getLow (pints) 목록을 반복 할 때 매우 느린 http : // docs이기 때문에 함수에 내장 된 Python을 사용해야합니다. python.org/library/functions.html, 비록이 경우에는 성능에 영향을 미치지 않지만 – user1462442

+0

@ user1462442 네,하지만 저는 그것이 다른 질문 일 수 있다고 생각합니다. OP가 직면하고있는 실제 문제로 제목을 업데이트했습니다. –

0

"변수가 할당하기 전에 참조"당신이 당신의 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 

들여 쓰기 : 또한 당신은 당신의 getPints 기능을 내부에 들여 쓰기를 확인해야 아직 존재하지 않는 변수를 사용하고 있다는 것을 의미합니다. 코드에서 문제는 다음 행입니다.

totalPints += pints[counter] 

totalPints가 처음 나타납니다. "+ ="구성은 정확히

totalPints = totalPints + pints[counter] 

과 동일하며, 파이썬이 반대하는 것은 올바른 일입니다. 이 문제를 해결하려면 루프를 시작하기 전에

totalPints = 0 

으로 변수를 초기화하십시오.

0

글쎄 그것은 쉬운 수정입니다. 변수를 추가하기 전에 변수를 지정하기 만하면됩니다.

totalPins = 0 

또는

totalPins = "" 

트릭을 할해야 루프에 들어가기 전에.

관련 문제