0
我正在嘗試查找列表中數字的均值。我從.txt文件獲取列表,然後通過我的函數。從文本文件中輸入數字的平均值
def find_mean(list):
sumofnums = sum(list)
avgofnums= sumofnums/len(list)
print(avgofnums)
def get_numbers_from_file(file_name):
file = open(file_name, "r")
strnumbers = file.read().split()
int_strnumbers = [int(i) for i in strnumbers
return (int, int_strnumbers)
mylist = get_numbers_from_file(input("Enter file name"))
print(find_mean(mylist))
但是,我得到操作數類型錯誤,即它說該文件是作爲字符串列表而不是ints返回的。
Traceback (most recent call last):
File "C:/Users/andrewr/Desktop/Mean_Module.py", line 15, in <module>
print(find_mean(mylist))
File "C:/Users/andrewr/Desktop/Mean_Module.py", line 3, in find_mean
sumofnums = sum(list)
TypeError: unsupported operand type(s) for +: 'int' and 'type'
我以爲我通過將strjumbers改爲int_strnumbers來解決了這個問題。
請幫助任何人都可以幫助我嗎?
平均和你錯過了']'線11 – Dadep
你可以只用'statistics.mean'而不是你自己的平均功能。 –