# while loop - sentinel menu

## Objective

After working through this lesson, you’ll be able to

* Write Python code using a while loop with a sentinel value to create a menu.

## Introductory Problem

Using a while loop, ask the user how many snaps they’ve sent each day for the past 5 days.  Print the sum of these numbers.

{% code title="intro.py" %}

```python
sum = 0
x = 0
while x < 5:
    snaps = int(input("How many snaps did you send" ))
    sum = sum + snaps
print(sum)
```

{% endcode %}

## while loop - sentinel menu

Previously, you learned how to write loops that read and processed a sequence of values until it reached a sentinel value.  Recall that a sentinel value marks the end of a data set, but it is not part of the data set. A loop that uses a sentinel value in this way is called a sentinel-controlled loop.  Today, you'll use a while loop with a sentinel value to create a menu system.  This is also known as a game loop.

![](https://729613953-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LVVKHMnpflHwghmbdgi%2F-Ld1-yevy4oSvuozWgO1%2F-Ld100SxyORCtHFnAUt_%2FScreen%20Shot%202019-04-21%20at%206.28.29%20PM.png?alt=media\&token=e04b7ce1-f16e-44c9-b6e3-5225cd4629df)

{% hint style="info" %}
The variable role of done is a One-Way Flag.
{% endhint %}

![](https://729613953-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LVVKHMnpflHwghmbdgi%2F-Ld1eF9Awda6rvYuMvqZ%2F-Ld1eH2L7uzani0hbIM-%2FScreen%20Shot%202019-04-21%20at%209.28.46%20PM.png?alt=media\&token=9c5d961f-75c0-4edb-b946-37aadb012dfa)

## Examples

### Example 1

Initialize the variable done to False. &#x20;

Create a while loop with the condition not done. &#x20;

Print these lines:

print("Menu")

print("S - Start")

print("Q - Quit")

If the user enters S, print "Starting Game".

Else if the user enters Q, print "Exiting Game", and assign done to True.

{% code title="example1.py" %}

```python
done = False
while not done:
    print("Menu")
    print("S - Start")
    print("Q - Quit")
    choice = input(":: ")
    if choice == "S":
        print("Starting Game!")
    elif choice == "Q":
        print("Exiting Game!")
        done = True
    else:
        print("Invalid Choice")
```

{% endcode %}

### **Example 2**

Create a new menu item "B - Bus Ride"\
Ask the user how many minutes they ride the bu&#x73;**.**\
If the user enters less than 30, print "not long", else print "too long".

{% code title="example2.py" %}

```python
done = False
while not done:
    print("Menu")
    print("S - Start")
    print("B - Bus Ride")
    print("Q - Quit")
    choice = input(":: ")
    if choice == "S":
        print("Starting Game!")
    elif choice == "Q":
        print("Exiting Game!")
        done = True
    elif choice == "B":
        bus = int(input("How long is your bus ride? "))
        if bus < 30:
            print("not long")
        else:
            print("too long")
    else:
        print("Invalid Choice")
```

{% endcode %}

### Example 3

Create a new menu item "N - Number"

Ask the user for a number.  If they enter an even number, print even.  Else if they print an odd number, print odd.

{% code title="example3.py" %}

```python
done = False
while not done:
    print("Menu")
    print("S - Start")
    print("B - Bus Ride")
    print("N - Number")
    print("Q - Quit")
    choice = input(":: ")
    if choice == "S":
        print("Starting Game!")
    elif choice == "Q":
        print("Exiting Game!")
        done = True
    elif choice == "B":
        bus = int(input("How long is your bus ride? "))
        if bus < 30:
            print("not long")
        else:
            print("too long")
    elif choice == "N":
        num = int(input("Number: "))
        if num % 2 == 0:
            print("even")
        else:
            print("odd")
    else:
        print("Invalid Choice")
```

{% endcode %}

## Conclusion

* In your own words, what is a sentinel?  What are real life examples of sentinels?
* Explain the role of the One-Way Flag variable when using a while loop with a sentinel value to create a menu.

<br>

\ <br>
