python - 令人困惑的名称错误

标签 python python-3.2

我有一个问题。我在下面运行了这个程序,并得到了一个不寻常的错误,表明名称“mean”未定义。这是错误:

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)

最佳答案

calculateMean() 函数的结果分配给 mean“本地”变量:

mean = 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))

关于python - 令人困惑的名称错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36815856/

相关文章:

在 Ubuntu 12.04 上安装 Python 3.2

python - 使用 MultiIndex 过滤

python - 用于比较的自定义 __lt__ 方法

python - Ubuntu 9.10 中奇怪的 pynotify 行为

pip - 无法使用 Python 3.2 创建 virtualenv。谁不知道? Pip 或 setuptools 或 Python 2.7?

mysql pyramid initialise db 使用有效数据生成无效语法

python - 从多个页面抓取表格并从链接添加数据

python - 如何从自定义库结束主函数?

python - 如何找到 Pypi 包的最新*兼容*版本?

python - 缓存和内存使用