hello,

i have to create a program that open a file, read the file, add the number form the files, and display the total sum.

Here is what i have

numbersFile = open('numbers.dat', 'r')
line = numbersFile.read()
for line in open("numbers.dat"):
    nums = [int(x) for x in line.split()]
#close file
numbersFile.close()
 
print nums

the number inside the file ( numbers.dat ) are 13, 15, 19, some how they are not adding up

thanks for the help guys

Recommended Answers

All 6 Replies

simple program, help

I have never understood this type of title. If you don't know how to do it, then how do you know it is simple. And if it is simple, then why do you need help?

Print the "nums" list inside the for() loop. It possibly contains more than you think it does. To add the numbers up you have to go through each list one by one, and add each number individually to a total. Python does have a built-in function to do this, but I am assuming that this is some type of homework and you are supposed to do it yourself.

Also, the first 2 lines of code do nothing. You can delete them.

I have never understood this type of title. If you don't know how to do it, then how do you know it is simple. And if it is simple, then why do you need help?

Print the "nums" list. It possibly contains more than you think it does. To add the numbers up you have to go through each list one by one and add each number individually to a total. Python does have a built-in function to do this, but I am assuming that this is some type of homework and you are supposed to do it yourself.

Also, the first 2 lines of code do nothing. You can delete them.

correct. you made your point.

Now, the function you are talking about is ( sum ) ?
that is what i'm trying to figure out, maybe you can help me.

Something you can look that may help you.

>>> s = '13, 15, 19'
>>> s = s.split(',')
>>> s
['13', ' 15', ' 19']
>>> [float(item) for item in s]
[13.0, 15.0, 19.0]
>>> sum([float(item) for item in s])
47.0
>>>

snippsat: thank you

now, is just not set numbers, i want to sum any number that is inside a file.

Just to put in one line.
You can try to understand the code and spilt it more upp.
If it a school task you have to explain and understand the code.

>>> sum([float(item) for item in i.split(',') for i in open("numbers.dat")])
47.0
>>>

Just to put in one line.
You can try to understand the code and spilt it more upp.
If it a school task you have to explain and understand the code.

>>> sum([float(item) for item in i.split(',') for i in open("numbers.dat")])
47.0
>>>

To make code more Pythonically correct, you would not produce list which you never use, so you would just sum the generator (this is to give you something to learn for correct Python style, not as example of beginners coding style)

print(sum(float(item) for item in i.split(',') for i in open("numbers.dat")))

Most correct still would be (but I am still in habbit of using the obove one liner):

with open("numbers.dat") as my_file:
    print(sum(float(item) for item in line.split(',') for line in my_file))

This quarantees the automatic closing of the file, even in case of errors in statements.

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.