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 menu
  • Examples
  • Example 1
  • Example 2
  • Example 3
  • Conclusion

Was this helpful?

  1. Loops

while loop - sentinel menu

PreviousProblemsNextProblems

Last updated 5 years ago

Was this helpful?

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.

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

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.

The variable role of done is a One-Way Flag.

Examples

Example 1

Initialize the variable done to False.

Create a while loop with the condition not done.

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.

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

Example 2

Create a new menu item "B - Bus Ride" Ask the user how many minutes they ride the bus. If the user enters less than 30, print "not long", else print "too long".

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

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.

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

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.