Python Classroom
  • Introduction
  • About Python Classroom
  • Python Cloud Options
    • CS50 IDE
      • CS50 IDE Overview
      • CS50 IDE and Python
      • CS50 IDE Debugging
      • CS50 IDE Shortcuts
      • Linux Bash Shell
      • Vim Mode
        • Vim Tutorial
    • CS50 Sandbox
    • PythonAnywhere
    • Repl.it
  • Python Curriculum Map
    • Pedagogy
      • Python Professional Development
      • Teaching Tips
      • Assessment Tips
      • Rubrics
      • Activities
        • Picture Activity
        • Map Activity
        • Crossing the Bridge
        • NIM
        • Mastermind
        • Cards
          • Card Deck Algorithms
          • Sorting Cards
        • River Crossing
          • Jealous Boyfriends
          • Cannibals and Priests
          • Family
          • Police and the Thief
          • Humans and Monkeys
          • Moving Money
        • Crossing the River
        • Traveling Salesperson
        • Logic Problems
        • CIA Crack the Code
        • IQ Test
        • Puzzles
    • AP Computer Science Principles Framework
  • Python Philosphy
    • How to Practice Python
  • microbit
  • Turtle Graphics
    • Turtle Examples
    • Turtle Activities
    • Turtle Maze Problems
    • Turtle Graphics with loops
    • Turtle Snake
    • Turtle Graphics with Conditionals
  • Output
    • Output Examples
    • Output Mistakes
  • Variables
    • Variable Data Type Examples
    • Variable Role Examples
    • Variables Mistakes
    • Variables Problems
  • Math
    • Math Examples
    • Math Mistakes
    • Math Problems
    • Math Self Check
  • Input
    • Input Examples
    • Input Mistakes
    • Input Problems
  • Decisions
    • if
      • if Examples
      • if Mistakes
      • if Problems
    • if else
      • if else Examples
      • if else Problems
      • if / if else Problems
    • if elif else
      • if elif else Examples
      • if elif else Problems
    • nested if
      • nested if Examples
      • nested if Problems
    • Logical Operators
      • Logical Operators Examples
    • Adventure Game Project
  • Loops
    • while loop - count up
      • Examples
      • Problems
    • while loop - countdown
      • Examples
      • Problems
    • while loop - sentinel value
      • Problems
    • while loop - sentinel menu
      • Problems
    • for loop - range (one argument)
      • Examples
      • Problems
    • for loop - range (two arguments)
      • Problems
    • for loop - range (three arguments)
      • Problems
  • Lists
    • Lists - Numbers
      • Problems
    • Lists - Strings
      • Problems
      • Shopping Project
  • Dictionaries
  • String Methods
  • Functions
    • Variable Scope
    • Functions - no parameters
    • Functions - one parameter
    • Functions - return
    • Functions - lists
  • Files
  • Classes
    • Inheritance
  • Python Projects
    • Adventure Game
    • Restaurant Project
    • Trivia Game
    • Family Tree Project
  • Raspberry Pi
    • Raspberry Pi Models
    • Raspberry Pi Setup
  • Roblox
  • Glossary
Powered by GitBook
On this page
  • Introduction
  • Introductory Problem
  • while loop
  • Variable Roles
  • while loop Examples
  • Example 1
  • Example 2
  • Example 3
  • Example 4
  • Conclusion

Was this helpful?

  1. Loops

while loop - countdown

PreviousProblemsNextExamples

Last updated 5 years ago

Was this helpful?

Introduction

Introductory Problem

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

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

while loop

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.

Variable Roles

Recall that a stepper variable iterates, or loops, a specific number of times.

Programmers usexorias a stepper variable.

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.

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

Example 2

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

What value can we initialize the stepper variable to?

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

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.

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

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.

    • print("Mins\tCalories")

    • "\t" means tab.

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

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.