Python 用户输入平均值

标签 python input average

我正在尝试完成一项学校作业,要求用户输入 3 个整数,然后我需要将这三个整数作为参数传递给名为 avg 的函数,该函数将返回这三个整数作为浮点值。

这是我到目前为止所想到的,但我收到了这个错误:

line 13, in <module>
    print (average)
NameError: name 'average' is not defined  

建议?

    a = float(input("Enter the first number: "))
    b = float(input("Enter the second number: "))
    c = float(input("Enter the third number: "))

    def avg(a,b,c):
        average = (a + b + c)/3.0
        return average


    print ("The average is: ")
    print (average)

    avg()

最佳答案

average 仅作为函数 avg 内的局部变量存在

def avg(a,b,c):
    average = (a + b + c)/3.0
    return average

answer = avg(a,b,c) # this calls the function and assigns it to answer

print ("The average is: ")
print (answer)

关于Python 用户输入平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22394320/

相关文章:

c++ - 临时转换输入防弹?

php - 你如何遍历 $_FILES 数组?

python - 12 月至 1 月的平均日期

java - 尝试计算使用 do-while 和 while 循环给出的输入的平均值,但它一直停止

sql - 窗口子句的解析函数和方法

python - 关闭 Bokeh 中的刻度线

python - SQL 一对多关系 - 查询以检索 JSON 格式的数据

javascript - 使 DIV 充当 INPUT 字段

python - 传递 bool 数组而不用 `f2py` 复制?

python - 在 Pyramid 中的 View 之间传递表单参数