-3

I need some help with the logic of a program. What I am trying to do:

Write a program with a loop that asks the user to enter a series of positive 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 the sum.

keep_going = ' '
max = ()
total = 0.0

print('This program will add numbers together until a negative number is entered.')
print('It will then show the total of the numbers entered.')

while keep_going != (-):
    number = int(input('Enter a number: '))
    total = total + number

print('The total is', total)

Where am I going wrong?

user3030048
  • 83
  • 1
  • 3
  • 11

2 Answers2

5

Use an infinite loop and test if the number just entered is smaller than 0:

total = 0

while True:
    number = int(input('Enter a number: '))
    if number < 0:
        break
    total = total + number

Only by testing the number just entered can you detect when a negative number has been entered.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
0

You never change the value of keep_going, so your loop will never terminate.

Andrew
  • 4,953
  • 15
  • 40
  • 58