Home > Net >  Program that separately adds positive and negative inputs and displays the two sums
Program that separately adds positive and negative inputs and displays the two sums

Time:03-05

I'm trying to solve this hw problem: Write a program that allows a user to keep inputting numbers until the number zero is entered. The program should keep track of two separate sums: The sum of all positive numbers entered and the sum of all negative numbers entered. Once the loop ends the program should display the two sums.

This is what I have so far:

number = int(input("Please enter a number, press 0 to end"))

 sum1 = 0
 sum2 = 0

 while number != 0:
     number = int(input("Please enter a number, press 0 to end"))

     if number > 0:
         sum1  = number

     else:

        sum2  = number


print("positive sum is", sum1)

print("negative sum is", sum2)

the problem i'm facing is that number needs to be defined in order to start the while loop, and then for the loop to keep asking the question, i need to define number inside the loop too and that messes up the count because the first user input is used just to start the loop and is not counted. How do i fix this?

CodePudding user response:

You can simply initialize number to something other than 0, so the number isn't asked twice at the start of the program.

sum1 = 0
sum2 = 0

number = -1
while number != 0:
    number = int(input("Please enter a number, press 0 to end"))

    if number > 0:
        sum1  = number
    else:
        sum2  = number

print("positive sum is", sum1)
print("negative sum is", sum2)

CodePudding user response:

Try doing this. You can delete the sample_input lines and uncomment your original line taking input from stdin:

        sample_input = iter([3, -3, 2, -2, 1, -10, 0])
        sum1 = 0 
        sum2 = 0
        number = 999
        while number != 0: 
            number = next(sample_input)
            #number = int(input("Please enter a number, press 0 to end"))
            if number > 0:
                sum1  = number
            else:
                sum2  = number
        print("positive sum is", sum1) 
        print("negative sum is", sum2)    

Sample output:

positive sum is 6
negative sum is -15

CodePudding user response:

Use the walrus:

sum1 = 0
sum2 = 0

while number := int(input("Please enter a number, press 0 to end")):
    if number > 0:
        sum1  = number
    else:
        sum2  = number

print("positive sum is", sum1)
print("negative sum is", sum2)

And I'd suggest sum_pos and sum_neg as better more meaningful variable names.

CodePudding user response:

Don't use the number test in the while condition. Check for it being 0 after getting the input and break out of the loop.

while True:
    number = int(input("Please enter a number, press 0 to end"))

    if number == 0:
        break
    elif number > 0:
        sum1  = number
    else:
        sum2  = number

I prefer this style because it doesn't require you to initialize the variable to a special value. Also, it allows the end value to be something that wouldn't be useful in the rest of the computation.

  • Related