多年来,我一直在摆弄这段代码,无法弄清楚如何让它通过 doctests。输出总是比正确答案少 1000。有没有一种简单的方法来更改此代码,以便它提供所需的输出?我的代码是:
def sum_numbers_in_file(filename):
"""
Return the sum of the numbers in the given file (which only contains
integers separated by whitespace).
>>> sum_numbers_in_file("numb.txt")
19138
"""
f = open(filename)
m = f.readline()
n = sum([sum([int(x) for x in line.split()]) for line in f])
f.close()
return n
文件中的值为:
1000
15000
2000
1138