所以我必须编写一个代码来读取我的输入文件名为“数字.txt“那是由数字1-10组成的,但是我如何让代码在输出文件中写下总和呢?”?。我的代码已经告诉了我总数,但如何生成输出文件“outputnumbers.txt文件“数字是1-10加上和吗?你知道吗
total = 0
with open('numbers.txt', 'r') as inp, open('outputnumbers.txt', 'w') as outp:
for line in inp:
try:
num = float(line)
total += num
outp.write(line)
except ValueError:
print('{} is not a number!'.format(line))
print('Total of all numbers: {}'.format(total))
尝试以下操作。
我刚刚添加了一行
outp.write('\n'+str(total))
,在for循环完成求和之后添加数字的和你知道吗数字.txt你知道吗
你知道吗outputnumbers.txt文件你知道吗
相关问题 更多 >
编程相关推荐