3

I have a program and reads from a .txt file that I have created using another python code. I'm having trouble finishing this one.

It needs to read the .txt and spit out the numbers in that file along with their sum. Here's what I've got so far:

 def main():
    total = 0
    myfile = open('numbers.txt', 'r')
    for line in myfile:
        amount = float(line)
        total += amount
    print('End of file')
    print('Numbers in file add up to ', format(total, ',.1f'), end='')
    myfile.close()

main()

I get the error msg:

ValueError: could not convert string to float: '11 13 11 7 7'
4
  • What's wrong? Your code is working so good. Commented Sep 19, 2015 at 1:17
  • Yes my codes work, but I can't get the random numbers to print out like 1 2 3 4 5 instead they print out 12345 Commented Sep 19, 2015 at 1:21
  • try to use print('Numbers in file add up to ', format(total, ',.1f'), end=' ')? (see that space) Commented Sep 19, 2015 at 1:21
  • tried that.. it prints decimals then. I just need whole numbers Commented Sep 19, 2015 at 1:26

2 Answers 2

5

Now, try this:

 def main():
    total = 0
    with open('numbers.txt', 'r') as myfile:
        for line in myfile:
            for i in line.split():
                amount = float(i)
                total += amount
        print(line.strip(), 'End of file')
        print('Numbers in file add up to ', format(total, ',.1f'), end='')
        print()

main()

Because line is one line, and that is '11 13 11 7 7'.

So float() can't convert one string like that to float. And these codes use split() to split that string to a list like['11', '13', '11', '7', '7']. Then use for to extract it.

And now, float() can convert '11', '13', etc. to float.

Sign up to request clarification or add additional context in comments.

3 Comments

YAY!!! Is there a way to add a line that prints out the numbers that are in that .txt file?
what is the output looks like? an example?
oh sorry.. the output looks that I would like it to look like is: 11 15 13 19 9 End of file Numbers in file add up to 67
2
import random

def main():
    myfile = open('numbers.txt', 'w')
    total = 0
    for count in range(3,8):
        file_size = random.choice(range(5,19,2))
        myfile.write(format(str(file_size) + ' '))
        total += file_size
    myfile.write(format(total))
    myfile.close()
main()

2 Comments

I'm not sure what you mean by separate codes. I see your above comment about the numbers being printed next to each other without spaces in between, but my file output shows spaces in between the numbers when I use my code. Or do you mean something different, now?
sorry maybe I could have used different wording. So I have 2 codes that basically do your 1 code if you will. My first code creates the .txt doc with the number then my 2nd code reads from that .txt and spits out the numbers and their sum. Does that make sense?

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.