so i'm trying to pull numbers from a file numbers.txt and add them together. The program can currently pull the numbers one at a time and print them spaced out on one line. I now need it to total all of the values. The numbers in the file are: 9 19 15 17 5 17 The total should be 82 but it will only add the two number 17's and output 34
def main():
numfile = open('numbers.txt', 'r')
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()
totalevery time through the loop. Try settingtotal = 0before thefor line instatement.