找到5个数字
查找数字的平均值
#import statistics
print("enter 5#s")
num1 = int(input())
num2 = int(input())
num3 = int(input())
num4 = int(input())
num5 = int(input())
numbers = [num1, num2, num3, num4, num5]
numsum = sum(numbers)
print("sum is:", numsum)
#Above list is working
#fix below
def Average(numbers):
return sum(numbers) / len(numbers)
print("AVG = ", round(Average, 2))
错误
追溯(最近一次通话):
文件“ main.py”,位于第15行
print(“ AVG =”,round(Average,2))
TypeError:类型函数dantnt define_round_method
最佳答案
最后一行(对print
的调用)
def Average(numbers):
return sum(numbers) / len (numbers)
print("AVG = ", round(Average, 2))
需要取消缩进(向左移动,在函数外部),并且需要使用
numbers
作为其参数来调用函数,如下所示:def Average(numbers):
return sum(numbers) / len (numbers)
print("AVG = ", round(Average(numbers), 2))
您的代码可以简化和改进,也许像这样:
num_list = []
for i in range(5):
s = input('Enter the {}. number: '.format(i+1))
n = int(s)
num_list.append(n)
the_sum = sum(num_list)
print('Sum:', the_sum)
the_avg = round(the_sum / len(num_list), 2)
print('Avg:', the_avg)
样品运行:
Enter the 1. number: 7
Enter the 2. number: 5
Enter the 3. number: 4
Enter the 4. number: 4
Enter the 5. number: 4
Sum: 24
Avg: 4.8
关于python - 列表中的平均数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56853050/