Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add numbers and exit with a sentinel

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.

like image 475
Baroness Sledjoys Avatar asked Oct 17 '15 14:10

Baroness Sledjoys


People also ask

What is a sentinel value example?

For example, in a loop algorithm that computes non-negative integers, the value “-1” can be set as the sentinel value as the computation will never encounter that value as a legitimate processing output. Also referred to as a flag value or a signal value.

What is a sentinel value in coding?

In computer programming, a sentinel value is a special value in the context of an algorithm which uses its presence as a condition of termination, typically in a loop or recursive algorithm.

What does sentinel mean in C++?

A "sentinel" in this context is a special value used to indicate the end of a sequence. The most common sentinel is \0 at the end of strings. A "sentinel while loop" would typically have the form: while (Get(input) != Sentinel) { Process(input); }


2 Answers

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.

like image 181
Markus Meskanen Avatar answered Sep 16 '22 20:09

Markus Meskanen


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)
like image 24
Mureinik Avatar answered Sep 16 '22 20:09

Mureinik