【发布时间】:2018-10-21 05:47:02
【问题描述】:
我运行这段代码:
file=open(filename, 'r')
line=filename.readline()
totallines=0
total=0
while line != "":
amount=float(line)
print(format(amount, '.2f'))
line=filename.readline()
totallines+=1
total+=amount
avg=total/totallines
print("The average of the numbers is", format(avg, '.2f'))
它给了我这个错误 AttributeError: 'str' object has no attribute 'read'
我不明白我做错了什么?
【问题讨论】:
-
filename是一个字符串。你应该做file.readline()
标签: python