Running Sum script

Discussion in 'Python' started by Jul, Sep 4, 2009.

  1. Jul

    Jul Guest

    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
     
    Jul, Sep 4, 2009
    #1
    1. Advertising

  2. Jul

    David Smith Guest

    Jul wrote:
    > 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
     
    David Smith, Sep 4, 2009
    #2
    1. Advertising

  3. Jul

    Jul Guest

    On Sep 4, 2:21 pm, Stephen Fairchild <> wrote:
    > Jul wrote:
    > > 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.

    >
    > Untested:
    >
    > with open("numbers.txt", "r") as f:
    > print sum(int(x) for x in f)
    > --
    > Stephen Fairchild


    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....
     
    Jul, Sep 4, 2009
    #3
  4. Jul

    David Smith Guest

    Jul wrote:
    > On Sep 4, 2:21 pm, Stephen Fairchild <> wrote:
    >> Jul wrote:
    >>> 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.

    >> Untested:
    >>
    >> with open("numbers.txt", "r") as f:
    >> print sum(int(x) for x in f)
    >> --
    >> Stephen Fairchild

    >
    > 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
     
    David Smith, Sep 4, 2009
    #4
  5. 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?

    On Fri, 04 Sep 2009 11:26:06 -0700, Jul <> wrote:

    > On Sep 4, 2:21 pm, Stephen Fairchild <> wrote:
    >> Jul wrote:
    >> > 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.

    >>
    >> Untested:
    >>
    >> with open("numbers.txt", "r") as f:
    >> print sum(int(x) for x in f)
    >> --
    >> Stephen Fairchild

    >
    > 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....




    --
    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)
     
    Rami Chowdhury, Sep 4, 2009
    #5
  6. On Sep 4, 2:52 pm, "Rami Chowdhury" <> wrote:
    > 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?
    >
    >
    >
    > On Fri, 04 Sep 2009 11:26:06 -0700, Jul <> wrote:
    > > On Sep 4, 2:21 pm, Stephen Fairchild <> wrote:
    > >> Jul wrote:
    > >> > 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.

    >
    > >> Untested:

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

    >
    > > 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....

    >
    > --
    > 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.
     
    Juli Dolzhenko, Sep 4, 2009
    #6
  7. Jul

    Maggie Guest

    On Sep 4, 4:37 pm, Tobiah <> wrote:
    > > 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


    try it where? code or terminal?

    thanks so much?
     
    Maggie, Sep 4, 2009
    #7
  8. Jul

    Tobiah Guest


    > 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
     
    Tobiah, Sep 4, 2009
    #8
  9. > 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.

    On Fri, 04 Sep 2009 13:37:10 -0700, Maggie <> wrote:

    > On Sep 4, 4:37 pm, Tobiah <> wrote:
    >> > 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

    >
    > try it where? code or terminal?
    >
    > thanks so much?




    --
    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)
     
    Rami Chowdhury, Sep 4, 2009
    #9
  10. Jul

    Guest Guest

    On Sep 4, 4:37 pm, Maggie <> wrote:
    > On Sep 4, 4:37 pm, Tobiah <> wrote:
    >
    > > > 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

    >
    > 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?
     
    Guest, Sep 4, 2009
    #10
  11. Jul

    Chris Rebert Guest

    On Fri, Sep 4, 2009 at 1:49 PM, <><><><><> wrote:
    > On Sep 4, 4:37 pm, Maggie <> wrote:
    >> On Sep 4, 4:37 pm, Tobiah <> wrote:
    >>
    >> > > 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

    >>
    >> 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?


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

    - Chris
    --
    http://blog.rebertia.com
     
    Chris Rebert, Sep 4, 2009
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Davisro
    Replies:
    2
    Views:
    6,618
    Davisro
    Apr 20, 2004
  2. NewTel

    GridView -Running sum

    NewTel, Feb 9, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    4,250
    NewTel
    Feb 9, 2006
  3. papaja
    Replies:
    3
    Views:
    2,753
    Chris R. Timmons
    Feb 12, 2006
  4. Benjamin Rutt
    Replies:
    2
    Views:
    944
    Peter Hansen
    Dec 20, 2005
  5. Replies:
    2
    Views:
    152
    Sean O'Halpin
    Dec 17, 2008
Loading...

Share This Page