Python loops with running total

  • Context: Python 
  • Thread starter Thread starter mr.me
  • Start date Start date
  • Tags Tags
    Loops Python Running
Click For Summary

Discussion Overview

The discussion revolves around a homework problem involving the creation of a Python function that maintains separate running totals for positive and negative integers entered by the user. The scope includes programming concepts, specifically loops and conditionals in Python.

Discussion Character

  • Homework-related
  • Technical explanation

Main Points Raised

  • One participant expresses confusion about their implementation of a function that should track positive and negative totals separately.
  • Another participant points out that the original code does not maintain separate totals for positive and negative values, suggesting that the user should input a number and the program should determine if it is positive or negative for tallying.
  • A participant questions how to implement the suggestion of having a single input prompt for the number, indicating a lack of understanding of this programming approach.
  • Another participant advises starting with a simpler version of the program that prompts for a number and processes it in a loop before adding complexity to meet the assignment's requirements.

Areas of Agreement / Disagreement

Participants generally agree on the need for the function to maintain separate totals for positive and negative values, but there is no consensus on how to implement the solution effectively. The discussion remains unresolved regarding the best approach to achieve the desired functionality.

Contextual Notes

There are limitations in the original code regarding the handling of input and the separation of totals, which have not been fully addressed. The discussion also highlights a potential misunderstanding of the assignment's requirements.

mr.me
Messages
49
Reaction score
0
I have a homework problem that I can't 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:
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
 
Technology news on Phys.org
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:
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
 
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.
 

Similar threads

Replies
1
Views
2K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 29 ·
Replies
29
Views
4K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 7 ·
Replies
7
Views
6K