# while loop - countdown

## Introduction

{% embed url="<https://docs.google.com/presentation/d/1vO70827sd3dxRknwshKedkLPBi2InJaTpkfqFXAuFJQ/edit?usp=sharing>" %}

## Introductory Problem

Ask the user what food they would like to eat everyday.  Using a while loop, print their favorite food 5 times.

{% tabs %}
{% tab title="intro.py" %}

```python
x = 0
food = input("What type of food do you like to eat? ")
while x < 5:
    print(food)
    x = x + 1
```

{% endtab %}
{% endtabs %}

## while loop <a href="#while-loop" id="while-loop"></a>

A while loop executes an indented block of code, or instructions, repeatedly while a condition is true. Previously, you learned about if statements that executed an indented block of code while a condition was true. You can think of a while loop like an if condition but the indented block of code executes more than once. Hence, a loop.

![](https://729613953-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LVVKHMnpflHwghmbdgi%2F-Ld0rS6P5Idtee4iNIqX%2F-Ld0rUkV8JUo6Cey0hu5%2FScreen%20Shot%202019-04-21%20at%205.46.50%20PM.png?alt=media\&token=62ea9271-2474-4f4d-b232-35242aefba03)

## Variable Roles

Recall that a stepper variable iterates, or loops, a specific number of time&#x73;**.**

![](https://729613953-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LVVKHMnpflHwghmbdgi%2F-Ld0rS6P5Idtee4iNIqX%2F-Ld0rn3l0wcDMfrTA1_g%2FScreen%20Shot%202019-04-21%20at%205.48.15%20PM.png?alt=media\&token=9afb913d-3a18-4016-a743-34a1e57e8468)

{% hint style="info" %}
Programmers us&#x65;**`x`**&#x6F;&#x72;**`i`**&#x61;s a stepper variable.
{% endhint %}

## while loop Examples

### Example 1

Rewrite the Do Now to print the user’s favorite food 5 times by decrementing the variable rather than incrementing.

Steps:

* Initialize the stepper variable x to 5.
* Ask the user what they like to eat everyday.
* Using a while loop, create a condition that will execute 5 times.
* Print variable.
* Decrement the stepper variable.

{% tabs %}
{% tab title="example1.py" %}

```python
x = 3
food = input("Favorite Food: ")
while x > 0:
    print(food)
    x = x - 1
```

{% endtab %}
{% endtabs %}

### Example 2

Using a while loop, print even numbers from 10 to 1.

What value can we initialize the stepper variable to?

{% tabs %}
{% tab title="example2.py" %}

```python
x = 10
while x > 0:
    print(x)
    x = x - 2
```

{% endtab %}
{% endtabs %}

### **Example 3**

Using a while loop, ask the user for a number 3 times.   Where in the program should we ask the user for the number?  Inside the loop, or outside the loop?

Create a temporary variable named squared that squares the number.  Create another temporary variable named cubed that cubes the number.

Print the values of squared and cubed.

{% tabs %}
{% tab title="example3.py" %}

```python
x = 3
while x > 0:
    num = int(input("Number: "))
    squared = num * num
    print("Squared:",squared)
    cubed = num ** 3
    print("Cubed:",cubed)
    x = x - 1

```

{% endtab %}
{% endtabs %}

### **Example 4**

You eat a Pepperoni Pizza slice which is 400 calories and now want to burn off these calories for 10 minutes.  You burn 11 calories per minute running. Create a chart to represent how many minutes you have left to exercise and how many calories you have left to burn off.

Steps:

* Create a variable named calories and initialize it to 400.
* Before the while loop, add the following lines to create your chart. &#x20;
  * print("Mins\tCalories")
  * "\t" means tab.

{% tabs %}
{% tab title="example4.py" %}

```python
print("Mins\tCalories")
calories = 400
x = 10
while x >= 0:
    print(x,"\t",calories)
    calories = calories - 11
    x = x - 1
```

{% endtab %}
{% endtabs %}

## Conclusion

* In your own words, what is a while loop?  What is a real life example when you count down?
* Explain the role of the stepper variable when the while loop counts down.

## &#x20;<br>
