我正在尝试完成一项学校作业,要求用户输入 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/