【发布时间】:2015-11-25 05:12:59
【问题描述】:
所以我试图从文件 numbers.txt 中提取数字并将它们加在一起。该程序目前可以一次拉出一个数字并将它们间隔打印在一行上。我现在需要它来汇总所有值。文件中的数字是:9 19 15 17 5 和 17。总数应该是 82 但它只会添加两个数字 17 并输出 34。
def main():
numfile = open('numbers.txt', 'r')
total = 0
for line in numfile:
line = line.rstrip('\n')
print (line, end=' ')
total = int(line)
total += total
print ("\nEnd of file")
print (total)
numfile.close()
main()
【问题讨论】: