Python 编写一个程序,计算文件中存储的所有数字的平均值 “” 平均数 ''' defmain(): 导入系统 尝试: numberText=打开(“numbers.txt”、“r”) 除IOE错误外: 打印(“在文件中找到非数字数据/n按enter键”) 除了(值错误): 打印(“在文件中找到非数字数据/n按enter键”) 其他: 总数=0 线数=0 line=numberText.readline() 一边排队="": 线路数+=1 TotalNumber+=int(行) line=numberText.readline() averageNumbers=总数量/线数 打印(“平均值为:”,平均数) 打印(“按回车键”) main()
如果信息不完整,最可能的原因是因为从文件中读取的行的末尾仍然有换行符,最后一行就是换行符Python 编写一个程序,计算文件中存储的所有数字的平均值 “” 平均数 ''' defmain(): 导入系统 尝试: numberText=打开(“numbers.txt”、“r”) 除IOE错误外: 打印(“在文件中找到非数字数据/n按enter键”) 除了(值错误): 打印(“在文件中找到非数字数据/n按enter键”) 其他: 总数=0 线数=0 line=numberText.readline() 一边排队="": 线路数+=1 TotalNumber+=int(行) line=numberText.readline() averageNumbers=总数量/线数 打印(“平均值为:”,平均数) 打印(“按回车键”) main(),python,Python,如果信息不完整,最可能的原因是因为从文件中读取的行的末尾仍然有换行符,最后一行就是换行符\n,因此while仍然为真,但是int(“\n”)失败。因此,要修改这两行: line = NumberTxt.readline().strip() # Strip leading and trailing spaces and newlines while line: # Not strictly necessary, but more Pythonic to test this way 我真的不知道
\n
,因此while
仍然为真,但是int(“\n”)
失败。因此,要修改这两行:
line = NumberTxt.readline().strip() # Strip leading and trailing spaces and newlines
while line: # Not strictly necessary, but more Pythonic to test this way
我真的不知道如何在她身上发帖。但是,我试图在python中使用Value Error和IOerror,但我的程序一直崩溃。请描述它是如何崩溃的,包括显示的任何错误消息的完整文本。