2016-04-23 5 views
2

Ich habe eine Frage. Ich habe dieses Programm unten ausgeführt und ich habe diesen ungewöhnlichen Fehler, der besagt, dass der Name "mean" nicht definiert ist. Hier ist der Fehler:Confusing Name Fehler

Traceback (most recent call last): 
    File "C:/Python32/Computer science stuff/PracticeTest 175-183.py", line 45, in <module> 
    main() 
    File "C:/Python32/Computer science stuff/PracticeTest 175-183.py", line 44, in main 
    displaySummary() 
    File "C:/Python32/Computer science stuff/PracticeTest 175-183.py", line 23, in displaySummary 
    print("List's mean: " + str(mean)) 
NameError: global name 'mean' is not defined 

Hier ist der Code. Was ist mein Fehler?

""" 
Programmer: Bertrand Zhu 
""" 

#Defining needed functions 
def askName(myName): 
    myName = myName.upper() 
    return myName 

def calculateMean(a, b, c, d, e): 
    sumNum = a + b + c + d + e 
    mean = round(sumNum, 2) 
    return mean 

def displaySummary(): 
    a = int(input("Input your first number please: ")) 
    b = int(input("Input your second number please: ")) 
    c = int(input("Input your third number please: ")) 
    d = int(input("Input your fourth number please: ")) 
    e = int(input("Input your fifth number please: ")) 
    calculateMean(a, b, c, d, e) 
    print("The values used: " + str(a) + " " + str(b) + " " + str(c) + " " +  str(d) + " " + str(e)) 
    print("List's mean: " + str(mean)) 
    print() 
    print() 

def programmerID(): 
    print() 
    print() 
    print("Programmer: THIS INFORMATION IS CLASSIFIED") 
    print("Roster #: 20") 
    print("Period: 7") 
    print() 
    print() 

def main(): 
    while True: 
     programmerID() 
     georgiePorgie = input("Enter your name: ") 
     if georgiePorgie.upper() == "QUIT": 
      break 
     else: 
      askName(georgiePorgie) 
      displaySummary() 
main() 
print("Goodbye, " + myName) 
+1

'print ("Liste der Mittelwert:" + str (Mittelwert))' bedeuten nicht in dieser Zeile definiert . Der Variablenumfang ist auf die Funktion 'calculateMean' beschränkt, von außerhalb kennt Ihr Programm die Variable nicht. – Delgan

+2

Probieren Sie 'mean = calculateMean (a, b, c, d, e)' – SparkAndShine

+1

'mean' ist eine freie Variable in' displaySummary'. Du hast wahrscheinlich vor, es an etwas zu binden. –

Antwort

5

Weisen Sie das Ergebnis der calculateMean() Funktion zum mean "lokale" Variable:

+0

Vielen Dank für das Problem – bertrand