Im taking Intro to computer programming and I have to designed a program in python that reads the numbers on a .txt file and then adds those numbers and display the sum to the user. Very simply program but I can't get it to work. I know sure how to write the code.

The program has to read all the lines in the file without me having to tell it how many lines I want it to read, so it has to have a while loop.

I hope someone can help me with this. Thank you

Very simply program but I can't get it to work. I know sure how to write the code.

You have to post code,how do you think we can help you?.
We are not mind readers.

but I can't get it to work.

Python always give you Traceback if something goes wrong,this is important to understand.
That you dont get the output you want can be a design fail in code.
But this no one can know,because you just say something dos not work.
An example of Traceback.

>>> k
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
NameError: name 'k' is not defined
>>> #So we need to define k
>>> k = 5
>>> k
5
>>>

what a teacher your are Snippsat. impressed!

I didn't post the code because Im not sure what code to write for this. I wrote the code to display the numbers on the file but I don't know what code I need to write to add the numbers on the file and then display the sum.

This is the code to display the numbers.

numbersFile = open ('numbers.txt', 'r')
for number in numbersFile:
    print number

What is type of number you are print? What should we do to find the total?

What is type of number you are print? What should we do to find the total?

That code just prints or displays any number on the file. I need to modified the code to add all the numbers in the file and display their sum

That was leading question for you to help to realize what you must do next. This program is simple one liner for experienced coders, but we can not give you ready solutions or we would hinder your learning Python instead of helping learning it.

That was leading question for you to help to realize what you must do next. This program is simple one liner for experienced coders, but we can not give you ready solutions or we would hinder your learning Python instead of helping learning it.

I understand and Im not looking for the answer I just need some help!

One more hint

print '1'+'1'
print int('1')+int('1')

One more hint

print '1'+'1'
print int('1')+int('1')

That hint didn't help much! :) I just started intro to computer programing and Im kinda confused. The program that I need to design reads the numbers from a file on the computer and then it adds those numbers and displays the sum to the user.

You can look at this example.

'''num.txt-->
1 2 3 4 5
'''

with open('num.txt')as f:
    for item in f:
        n = item.strip().split()
        print sum((int(i) for i in n)) #15

If you use print n before last line you see
So we have stript of newline(\n) and split the number up.
As you see now all number are string.
On the last line convert to integer and use sum() to calulate those numbers.

commented: Good teaching (some 'new reputation') +13

You can look at this example.

'''num.txt-->
1 2 3 4 5
'''

with open('num.txt')as f:
    for item in f:
        n = item.strip().split()
        print sum((int(i) for i in n)) #15

If you use print n before last line you see
So we have stript of newline(\n) and split the number up.
As you see now all number are string.
On the last line convert to integer and use sum() to calulate those numbers.

Thank makes it a little bit better but I don't need to print the numbers on the file for this program... I just need to add the numbers on the file and print their sum. I just showed the other code but it doesn't have to be included in the program. I was just saying that I could make it show the numbers but not add them

Thank makes it a little bit better but I don't need to print the numbers on the file for this program...

Yes you dont need to print number,i tok it with just to explain how this work.

Yes you dont need to print number,i tok it with just to explain how this work.

Hey man I still haven't been able to figure this thing out! any help

Post an effort of yours using our suggestions and error you are getting. Include the test input file.

Hey guys Im still trying to figure this out... I need to turn this in today and Im still unable to figure it. I changed a few things on the code but still nothing. Anybody can help me.?

Im not getting an error its just not adding correctly.

numbersFile = open ('numbers.txt', 'r')
line = numbersFile.readline()
while line !='':
   sum = line + line 
   line = numbersFile.readline()

print "The sum of the numbers is ",(sum)

You are not adding you are joining lines together.

You are not adding you are joining lines together.

Hmmm what Im I doing wrong? can you guide me in the right direction. Im really having a hard time with this simple program.

You got it from the snippsat's post which I now upvote.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.