def main():
filename = input("Enter a file name: ")
with open(filename) as f:
data = [int(line) for line in f]
if len(data) > 2:
print('The smallest number in the file is: ', + min(data))
print('The largest number in the file is: ', "{:,}".format(+ max(data)))
print('The total sum of the number in the file is: ', "{:,}".format(sum(data)))
print('The average from the numbers in the file is: ', "{:,.2f}".format(sum(data)/len(data)))
elif len(data) == 1:
print ('There is only one number in your file to process.')
else:
print('There are no numbers in your file to process.')
f.close()
main()
根据您的评论,我认为问题出在您的数据上,为了解决这个问题,您可以使用以下内容:
lines = filter(None, (line.strip() for line in f))
data = [int(line) for line in lines if line.isnumeric()]
请注意,这只解决了您遇到的异常——我还没有完成其余的代码并验证逻辑。