Running Sum script
dns4 at cornell.edu
Fri Sep 4 20:46:32 CEST 2009
> On Sep 4, 2:21 pm, Stephen Fairchild <someb... at somewhere.com> wrote:
>> Jul wrote:
>>> I have a .txt file that is in this format --
>>> ...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.
>> with open("numbers.txt", "r") as f:
>> print sum(int(x) for x in f)
>> Stephen Fairchild
> thats what i have so far --
> 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
> 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.
More information about the Python-list