Running Sum script

J

Jul

hello,

I have a .txt file that is in this format --

12625
17000
12000
14500
17000
12000
17000
14500
14500
12000
....and so on...

i need to create a python script that will open this file and have a
running sum until the end of file.

it sounds really simple its just for some reason i am having problem
with it.

i would really appreciate your help
 
D

David Smith

Jul said:
hello,

I have a .txt file that is in this format --

12625
17000
12000
14500
17000
12000
17000
14500
14500
12000
...and so on...

i need to create a python script that will open this file and have a
running sum until the end of file.

it sounds really simple its just for some reason i am having problem
with it.

i would really appreciate your help

It is really simple. Can you post the code you've written so far?

--David
 
J

Jul

Untested:

with open("numbers.txt", "r") as f:
print sum(int(x) for x in f)

thats what i have so far --

#!/usr/bin/python

import os.path

#open up the file
formisanoOpen = open("formisano_num.txt", "r")

#read in all the data into a list
readData = formisanoOpen.readLines()

#set up a sum
sum = 0;

#begin a loop
for trial in readData:

#the next line is indented (YA doesn't indent)
sum += int(trial)

#loop is over, so unindent
#report the sum
print sum


end

but it doesnt want to run for some reason....
 
D

David Smith

Jul said:
thats what i have so far --

#!/usr/bin/python

import os.path

#open up the file
formisanoOpen = open("formisano_num.txt", "r")

#read in all the data into a list
readData = formisanoOpen.readLines()

#set up a sum
sum = 0;

#begin a loop
for trial in readData:

#the next line is indented (YA doesn't indent)
sum += int(trial)

#loop is over, so unindent
#report the sum
print sum


end

but it doesnt want to run for some reason....

.... ok ... what do you get. If it's an error, please post the stack
trace. Please help us help you -- provide details.

--David
 
R

Rami Chowdhury

Could you let us know what kind of error you are getting?

I don't know if this is your error, but this line won't run:
readData = formisanoOpen.readLines()

Since Python is case-sensitive, you would need a lower-case 'l' in
'readlines()' -- perhaps that would solve your problem?
 
J

Juli Dolzhenko

Could you let us know what kind of error you are getting?

I don't know if this is your error, but this line won't run:


Since Python is case-sensitive, you would need a lower-case 'l' in
'readlines()' -- perhaps that would solve your problem?












--
Rami Chowdhury
"Never attribute to malice that which can be attributed to stupidity" --
Hanlon's Razor
408-597-7068 (US) / 07875-841-046 (UK) / 0189-245544 (BD)

in the terminal i get a very strange "permission denied" error that
might not have anything to do with the code. I checked permissions for
the file and they are set to "read and write" so, again, I am really
not sure what going wrong.
 
T

Tobiah

in the terminal i get a very strange "permission denied" error that might
not have anything to do with the code. I checked permissions for the file
and they are set to "read and write" so, again, I am really not sure what
going wrong.

Try:

python myfile

Or

chmod +x myfile
./myfile
 
R

Rami Chowdhury

try it where? code or terminal?

Please try these in the terminal -- the permission denied error may be due
to your shell not being able to execute the Python script, instead of your
Python script not being able to open the data file.
 
G

Guest

try it where? code or terminal?

thanks so much?

fantastic...! thank you so much..now i finally have my errors - which
are the following --

File "running_sum_formisano.py", line 18
sum+= int(trial)
^
IndentationError: expected an indented block

how would this be fixed?
 
C

Chris Rebert

fantastic...! thank you so much..now i finally have my errors - which
are the following --

 File "running_sum_formisano.py", line 18
   sum+= int(trial)
     ^
IndentationError: expected an indented block

how would this be fixed?

Have you even read the Python tutorial? Please do so if you haven't already..
Indent the line by 4 spaces.

- Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top