您应该首先将从文件检索的数据转换为整数,因为数据列表包含的字符串不是数字。在转换为整数后,可以很容易地找到平均值:
为什么需要转换为int?
>>> '2' > '10' #strings are compared lexicographicallyTrue
码:
def summaryStats(): filename = input("Enter a file name: ") with open(filename) as f: data = [int(line) for line in f] print("The minimum value is ", min(data)) print("The maximum value is ", max(data)) print("The average value is ", sum(data)/len(data))
输出:
Enter a file name: abc1The minimum value is 255The maximum value is 893The average value is 550.6666666666666
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)