1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Python loops with running total

  1. Oct 29, 2011 #1
    I have a homework problem that I cant figure out, can someone help

    --Create a function called sums that will prompt the user to enter integer values (either positive or negative). The function should keep separate running totals of the positive values and the negative values. The user should be allowed to continue entering values until he/she enters a zero to stop.


    Here's what I got and it doesn't work

    Code (Text):

    number=int(raw_input("Enter and a positive or negative integer: "))
    def sums(number):
          while (number>0):
                posnumber= int(raw_input("Enter another number or 0 to quit: "  ))
                number=number+posnumber
                print "The positive total is", number
               
          while (number<0):
                negnumber=int(raw_input("Enter another number or 0 to quit: "  ))
                number=number+negnumber
                print "The negative total is", number
    it just runs the loop under the first iteration, I'm confused as to what to do to correct it
     
  2. jcsd
  3. Oct 29, 2011 #2

    NascentOxygen

    User Avatar

    Staff: Mentor

    One of the instructions is The function should keep separate running totals of the positive values and the negative values. Your code is not doing this. Once you fix this shortcoming, your other problem might vanish. :smile:

    Perhaps you misunderstand the [probable] description. Here's what I reckon one detail is: input any number, for each input your program must examine whether that input is positive or negative and add the number to a running tally of positives or negatives, respectively.

    Your sequence of steps is so wrong that I think you should put it aside and start again. Write your code so that the line "int(raw_input("Enter a number .... or 0 to quit: " " occurs only once. That is very important for good programming.
     
    Last edited: Oct 29, 2011
  4. Oct 29, 2011 #3
    nascent said:"Write your code so that the line "int(raw_input("Enter another number or 0 to quit: " " occurs only once. That is very important for good programming."

    I don't know how to do that
     
  5. Oct 29, 2011 #4

    NascentOxygen

    User Avatar

    Staff: Mentor

    You start off by writing a short program which does the basis of what you ultimately need for your final program. The essential part is to loop through statements which achieve the following outcomes:

    :loop
    ... prompt for a number
    ... if the number is 0 then prepare to quit the loop
    ... print that number

    Use just one loop for inputting and processing all the numbers. Only when you have this much working properly should you start to flesh it out to do what the specifications ask.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook