2
我有一個問題。我在下面運行這個程序,我得到了這個不尋常的錯誤,說'名字'的意思是沒有定義。這裏是錯誤:令人困惑的名稱錯誤
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
這是代碼。我的錯誤是什麼?
"""
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)
'打印( 「列出的意思是:」 + STR(平均))'的意思是不是在該行定義。變量作用域限於函數'calculateMean',從它的外部,程序「不知道」該變量。 – Delgan
嘗試'mean = calculateMean(a,b,c,d,e)' – SparkAndShine
'mean'是'displaySummary'中的一個自由變量。你可能打算把它綁定到某個東西上。 –