while loop - sentinel menu
Last updated
Was this helpful?
Last updated
Was this helpful?
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.
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.
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.
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.
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".
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.
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.