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
  • Objective
  • Introductory Problem
  • while loop (sentinel)
  • Examples
  • Example 1
  • Example 2
  • Example 3
  • Example 4
  • Conclusion

Was this helpful?

  1. Loops

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

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

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

Use a variable named data to store the input value.

Don't use a float for equality checking in the condition because the values are approximated;.

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 0.

example1.py
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)

The input value 0 is the sentinel value for these loops.

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 numbers.

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

example2.py
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)

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 input.

example3.py
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)

Example 4

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

example4.py
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)

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.

PreviousProblemsNextProblems

Last updated 6 years ago

Was this helpful?