8

My assignment is to add up a series of numbers using a loop, and that loop requires the sentinel value of 0 for it to stop. It should then display the total numbers added. So far, my code is:

total = 0
print("Enter a number or 0 to quit: ")
while True:
    number = int(input("Enter a number or 0 to quit: "))
    print("Enter a number or 0 to quit: ")
    if number == 0:
        break
        total = total + number
        print ("The total number is", total)

Yet when I run it, it doesn't print the total number after I enter 0. It just prints "Enter a number or 0 to quit", though it's not an infinite loop.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
Baroness Sledjoys
  • 121
  • 1
  • 4
  • 9

5 Answers5

6

The main reason your code is not working is because break ends the innermost loop (in this case your while loop) immediately, and thus your lines of code after the break will not be executed. This can easily be fixed using the methods others have pointed out, but I'd like to suggest changing your while loop's structure a little. Currently you are using:

while True:
    if <condition>:
        break

Rather than:

while <opposite condition>:

You might have a reason for this, but it's not visible from the code you've provided us. If we change your code to use the latter structure, that alone will simplify the program and fix the main problem. You also print "Enter a number or 0 to quit:" multiple times, which is unnecessary. You can just pass it to the input and that's enough.

total = 0
number = None
while number != 0:
    number = int(input("Enter a number or 0 to quit: "))
    total += number  # Same as: total = total + number
print("The total number is", total)

The only "downside" (just cosmetics) is that we need to define number before the loop.

Also notice that we want to print the total number after the whole loop is finished, thus the print at the end is unindented and will not be executed on every cycle of the while loop.

Markus Meskanen
  • 19,939
  • 18
  • 80
  • 119
  • 3
    This, and the other answers, take advantage of the fact that the sentinel value is 0, and thus it is safe to unconditionally add the input number to the total, and check afterward if the loop should exit. That works, but it's perhaps a little too clever - if the sentinel value were someday to change to, say, -1, it might be easy to miss that we'd have to not only change the test `number != 0` to `number != -1`, but actually change the whole structure of the loop. – Nate Eldredge Oct 17 '15 at 17:53
3

You should sum the numbers inside the loop even if they aren't zeros, but print the total after the loop is over, not inside it:

total = 0
while True:
    number = int(input("Enter a number or 0 to quit: "))
    total = total + number
    if number == 0:
        break

print ("The total number is", total)
Mureinik
  • 297,002
  • 52
  • 306
  • 350
2
total = 0
while True:
    number = int(input("Enter a number or 0 to quit: "))
    if number == 0:
        break

    total = total + number

print("The total number is", total)

If you put break before your other code, then the loop will be ended and your code after that break will not run.

And by the way, you can use try...except to catch the error if user didn't enter a number:

total = 0
while True:
    try:
        number = int(input("Enter a number or 0 to quit: "))
    except ValueError:
        print('Please enter a number')
        continue

    if number == 0:
        break

    total = total + number

print("The total number is", total)
Remi Guan
  • 21,506
  • 17
  • 64
  • 87
  • 1
    Correct. It doesn't matter. But some may consider it better style to do the printing outside the input processing loop. Another minor improvement is to do the addition after the `if` test. Sure, adding zero doesn't change anything, and it's only one wasted instruction, but it is better style to avoid doing useless stuff when it's easy to avoid it by ordering the code differently. – PM 2Ring Oct 17 '15 at 14:27
  • @PM2Ring Got it, I've just edited my answer, what about now? – Remi Guan Oct 17 '15 at 14:32
  • 1
    Lovely! However, consider what happens in your `try: ... except` version if an invalid number is entered. You may want to use `continue`... – PM 2Ring Oct 17 '15 at 14:35
  • @PM2Ring Oops, forgot that :P – Remi Guan Oct 17 '15 at 14:40
2

If the number is 0, the first thing you are doing is break, which will end the loop.

You're also not adding the number to the total unless it's 0, which is not what you're after.

while True:
    number = int(input("Enter a number or 0 to quit: "))
    total = total + number
    if number == 0:
        break
print ("The total number is", total)    
Simon Fraser
  • 2,758
  • 18
  • 25
2

You were very near, but you had some indentation problem.

Firstly, why all these print statements? I guess you are trying to print it before taking input. For this, the below line will be enough.

number = int(input("Enter a number or 0 to quit: "))

Secondly, differentiate between what you want to do, when only the number==0 and what to do in every iteration.

You want to use the below instruction in every iteration as you want every number to be added with total. So, keep it outside if block.

total = total + number

And when number==0, you first want to print something and then break the loop.

if number == 0:
        print ("The total number is", total)
        break

Make sure you are adding with total first and then checking the if condition, because once you break the loop, you just can't add the number to total later.

So, the solution could be like that,

total = 0
while True:
    number = int(input("Enter a number or 0 to quit: "))
    total = total + number
    if number == 0:
        print ("The total number is", total)
        break
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ahsanul Haque
  • 10,676
  • 4
  • 41
  • 57