使用函数,如何打印从文件中读取的PAY列表的最低、最高和平均值?
try:
text_file = open ("Pay.txt", "w")
text_file.writelines(Pay)
text_file.close()
except (IOError):
print 'Error opening/writing Pay.txt'
try:
text_file= open("Pay.txt","r")
PAY_= text_file.readlines()
text_file.close()
PAY_.sort()
我从来没有设置过这样的东西,谁能让我开始吗?我会提前感谢你的回复。记住,我是新来的,我不知道你们是怎么做事的……
假设每行只有一个数字:
numbers = [float(line) for line in open('Pay.txt') if line.strip()]
if numbers:
print 'min', min(numbers)
print 'max', max(numbers)
print 'avg', sum(numbers) / len(numbers)
else:
print 'file is empty or all lines are blank'