Python loops with running total

In summary, the conversation discusses creating a function called sums that prompts the user to enter integer values and keeps separate running totals of positive and negative values. The function should continue until the user enters 0 to stop. The person asking for help has attempted to write the code, but it is not functioning correctly. They are advised to start with a simple program that loops through statements to prompt for numbers, recognize when the user enters 0, and print the number. This will help with the overall structure and organization of the code.
  • #1
mr.me
49
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
  • #2
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:
  • #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
 
  • #4
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.
 
  • #5


Hello,

First, it's important to understand the purpose of a loop in programming. A loop allows us to repeat a set of instructions until a certain condition is met. In this case, we want the loop to continue until the user enters a 0 to stop.

In your current code, you have two separate while loops that will only run once. This is because the condition for the first loop is number>0, and once the user enters a positive number, the condition is no longer met and the loop ends. The same goes for the second loop with the condition number<0.

To solve this problem, we can use a single while loop with an if statement inside to check for the positive or negative value. Here is an example:

def sums():
pos_total = 0
neg_total = 0
number = int(input("Enter a positive or negative integer: "))
while number != 0:
if number > 0:
pos_total += number
else:
neg_total += number
number = int(input("Enter another number or 0 to quit: "))
print("The positive total is", pos_total)
print("The negative total is", neg_total)

sums()

In this code, we first initialize two variables to keep track of the positive and negative totals. Then, we use a while loop to continuously prompt the user for a number until they enter 0 to stop. Inside the loop, we use an if statement to check if the number is positive or negative, and then add it to the appropriate total. Finally, we print out the totals at the end.

I hope this helps you understand how to approach this homework problem. Remember, loops can be tricky but with practice and patience, you'll be able to solve them. Good luck!
 

Related to Python loops with running total

What is a Python loop with running total?

A Python loop with running total is a programming technique that allows for the accumulation of values in a variable as the loop iterates through a sequence of data. This is useful for calculating totals, averages, or other cumulative values.

How do I create a Python loop with running total?

To create a Python loop with running total, you can use a for or while loop and keep track of the running total by adding to a variable with each iteration. For example, if you have a list of numbers, you can use a for loop to iterate through the list and add each number to a total variable.

What is the difference between a running total and a regular total in Python loops?

A regular total in a Python loop is the sum of all the values in a sequence at the end of the loop. A running total, on the other hand, is the cumulative sum of all the values at each step of the loop. This means that a running total will change with each iteration, while a regular total will only be calculated once at the end of the loop.

Can I use a Python loop with running total for non-numerical values?

Yes, a Python loop with running total can be used for any type of value as long as the values can be added together. For example, if you have a list of strings, you can use a for loop to concatenate them into a single string as a running total.

How can I reset a running total in a Python loop?

To reset a running total in a Python loop, you can simply reassign the variable used for the total to its initial value. This will clear the previous total and allow it to start accumulating again as the loop continues.

Similar threads

  • Programming and Computer Science
Replies
9
Views
833
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
29
Views
2K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
7
Views
553
  • Programming and Computer Science
Replies
16
Views
2K
  • Programming and Computer Science
Replies
17
Views
1K
Back
Top