0

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()

3
  • OK, thanks. I've removed the total = 0 and turned line into an integer.. but the program is only adding 2 numbers from the file not all 6.. but it will print all 6 Commented Nov 25, 2015 at 4:47
  • No, you're still reinitializing total every time through the loop. Try setting total = 0 before the for line in statement. Commented Nov 25, 2015 at 4:56
  • Ok thanks. I tried that. i'm still getting 34 as an answer instead of 82 Commented Nov 25, 2015 at 4:59

5 Answers 5

1

You have two problems. The first is as other answers have said - you are reading in strings. You need to convert those to a numeric data type. This is either int or float (or if you are doing monetary values, I recommend using decimal).

Very simply, this can be done like so:

total += float(line)

Second, you are resetting total to 0 each time you go through your for loop. Thus, at the end of your loop, total will equal the last value you read.

To correct this, pull your total = 0 out of the loop:

total = 0
for line in numfile:
    ...
Sign up to request clarification or add additional context in comments.

Comments

0

convert the string to an int:

total += int(line)

Comments

0

You'll need to convert line to an integer. At the moment, you're trying to add a string to an integer, which isn't possible. Simply wrap line in int(), like this:

total += int(line)

Comments

0

The error tells you the exact problem: you're adding an int and a str. The int is total and the str is line. Everything from a file is of type str. To fix the error, do total += int(line).

You will also need to initialize total before you start the loop, or it'll keep resetting to zero and leave you with just the final value.

Overall, you can streamline the code as follows:

with open('numbers.txt') as f:
    print(sum(map(int, f)))

This maps the file iterator to ints, then adds them with sum, then prints it with print.

Comments

0

You need to initialize total before the loop, not inside it. You're also doubling total instead of accumulating it. Try this:

def main():

numfile = open('numbers.txt', 'r')

total = 0  # initialize here

for line in numfile:
    line = line.rstrip('\n')

    print (line, end=' ')
    total += int(line)  #/ accumulate here

print ("\nEnd of file")   
print (total)

numfile.close()

main()

Note that total is modified once before the beginning of the loop and once inside the loop.

4 Comments

i have run the program with total initialized above the loop, and with "total = 0" removed. I am still getting an output of 34 but not 82.
Compare my solution to yours line-by-line and see if you can spot the difference.
Awesome. I appreciate your help. I am new to this site. Is their any way you get credit for helping?
Mark my answer "Accepted" by clicking on the big check mark to the left of it. Glad I could help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.