为了账号安全,请及时绑定邮箱和手机立即绑定

python 文件循环

python 文件循环

宝慕林9499331 2018-01-23 22:53:08
def main():    fileName = input ("What file are the numbers in? ")     infile = open (fileName, 'r' )    sum =0.0    count =0    line = infile.readline ( )     while line != " ":        sum=sum+eval(line)        count=count+1        line=infile.readline()    print ("\nThe average of the numbers is")main()What file are the numbers in? C:\Users\john\Documents\ze.txtTraceback (most recent call last):  File "C:/Users/john/AppData/Local/Programs/Python/Python36-32/s.py", line 12, in <module>    main()  File "C:/Users/john/AppData/Local/Programs/Python/Python36-32/s.py", line 8, in main    sum=sum+eval(line)  File "<string>", line 1        ^SyntaxError: unexpected EOF while parsing哪里出错了?是sum哪里吗,为什么错了?
查看完整描述

1 回答

?
慕仔3118017

TA贡献16条经验 获得超5个赞

   while line != " ": 

改成while line:就可以了,当读到eof时line为None。而不是空格

查看完整回答
反对 回复 2018-01-23
  • 1 回答
  • 0 关注
  • 1425 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信