1

Instructions: Create a program that asks a user to enter a series of numbers. The user should enter a negative number to signal the end of the series. After all the positive numbers have been entered, the program should display their sum.

I am using Python 2, Python IDLE

I'm using a while loop for this assignment. So far, I made a program that is saying, while the user enters a positive number under the while loop, collect that number and keep adding it until the user enters a negative number. I am trying to find a way to include the first user input into the program.

print('This program calculates the sum of the numbers entered and ends 
after inputting a negative number')
total = 0.00
number = float(input('Enter a number: '))
while number >= 0:
    print('Enter another positive value if you wish to continue. Enter a 
    negative number to calculate the sum.')
    number = float(input('Enter a number: '))
    total = total + number
print('The sum is', total)
2
  • Rather than initializing total with total = 0.0, set total to first number (just before while loop). Commented Oct 18, 2019 at 4:02
  • First input could be a negative number, then total would be wrong. User might want to exit on first input. Commented Oct 18, 2019 at 4:17

2 Answers 2

4

Have reduced your code to the below.
Performs checking of input in while loop and exits upon negative value.

total = 0.00

while True:
    print('Enter another positive value if you wish to continue. Enter a negative number to calculate the sum.')
    number = float(input('Enter a number: '))
    if number >= 0: # Check for positive numbers in loop
        total += number
    else:
        break
print('The sum is', total)
Sign up to request clarification or add additional context in comments.

Comments

0

I assume you're a beginner, so firstly welcome to Python! I hope you're having fun. Now, as far as your code goes, there are two things I noticed off the bat:

  1. You can simply put this 'Enter another positive value if you wish to continue. Enter a negative number to calculate the sum.' inside your input, why bother with an extra print statement?
  2. You don't have to call the input() function twice.

Here's how I'd do it:

print('This program calculates the sum of the numbers entered and ends 
after inputting a negative number')
total = 0.00
while True:

   number = float(input("Enter a positive number(Negative number if you wish to terminate program"))
   if number >=0:
       total += number #equivalent to total=total + sum
   else:
       break # break out of while loop

print('The sum is', total)

P.S - Why are you using Python 2 btw ?

1 Comment

Its for an intro class and we were asked to use python 2. But I am still trying to maneuver through. I had 2 inputs because of the initial input and the rest are for the following inputs until the user inputs a negative number. But it turns out I just needed to switch the total and the second input statement. But this way definitely makes sense. Also, Im not sure of the differences of Python yet but hopeful to learn about it in the future

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.