# while loop - sentinel value

## Objective

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

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

## Introductory Problem

Using a while loop, ask the user for the minutes of their bus/car ride 3 times.  Print the sum of these numbers

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

```python
x = 3
sum = 0
while x > 0:
    ride = int(input("How long? "))
    sum = sum + ride
print("Sum:",sum)
```

{% endcode %}

## while loop (sentinel)

Previously, you learned how to write loops that read and process a sequence of values. Today you will learn about while loops with sentinel values. A sentinel value denotes the end of a data set, but it is not part of the data. A loop that uses a sentinel value is called a sentinel-controlled loop.

|                           |                                                |
| ------------------------- | ---------------------------------------------- |
| 1 \<variable = input>     | Ask the user for a value                       |
| 2 while \<condition>:     | while loop executes while condition is True    |
| 3      \<do something>    | do something inside the while loop             |
| 4     \<variable = input> | Ask the user for a value again                 |
| 5 \<do something>         | (optional) do something outside the while loop |

{% hint style="info" %}
Use a variable named data to store the input value.
{% endhint %}

{% hint style="warning" %}
Don't use a float for equality checking in the condition because the values are approximated;.  &#x20;
{% endhint %}

## Examples

### Example 1

Using a while loop, ask the user how long their bus ride was this month until they enter 0.  Print the sum of these numbers.

Steps:

* Initialize a variable sum to 0.
* Ask "How long? (enter 0 to end): “
* Create a condition that will execute until the user enters 0.
* Sum the inputs
* Print the sum after the user enters &#x30;**.**

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

```python
sum = 0
data = int(input("How long? (enter 0 to end) "))
while data != 0:
    sum = sum + data
    data = int(input("How long? (enter 0 to end) "))
print("Sum:",sum)
```

{% endcode %}

{% hint style="info" %}
The input value 0 is the sentinel value for these loops.
{% endhint %}

### Example 2

Using a while loop, ask the user for the length of their bus/car ride until the user enters 0.  Print the average of these number&#x73;**.**

Initialize a counter variable to 0.  Every time a number is entered, increment the variable.

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

```python
sum = 0
x = 0
data = int(input("How long? (enter 0 to end) "))
while data != 0:
    sum = sum + data
    x = x + 1
    data = int(input("How long? (enter 0 to end) "))
average = sum / x
print("Average:",average)
```

{% endcode %}

### Example 3

Using a while loop, ask the user for the length of their bus/car ride until the user enters 0.  Print the maximum of these numbers.  Initialize max to the value of the first inpu&#x74;**.**

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

```python
sum = 0
x = 0
data = int(input("How long? (enter 0 to end) "))
while data != 0:
    if x == 0:
        max = data
    else:
        if data > max:
            max = data
    x = x + 1
    data = int(input("How long? (enter 0 to end) "))
print("Max:",max)
```

{% endcode %}

### Example 4

Using a while loop, ask the user to enter numbers until they enter 0.  Print the total number of even number&#x73;**.**

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

```python
evens = 0
data = int(input("Give me a number (enter 0 to end): "))
while data != 0
    if data % 2 == 0:
        evens = evens + 1
    data = int(input("Give me a number (enter 0 to end): "))
print("Evens:",evens)
```

{% endcode %}

## Conclusion

* What is an example of when you would use a while loop that does not have a fixed number of inputs?
* Explain the role of the sentinel variable.

## <br>

<br>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://www.pythonclassroom.com/loops/while-loop-sentinel.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
