Estimated time
10 minutes
Level of difficulty
Easy
Objectives
Familiarize the student with:
- using the
break
statement in loops; - reflecting real-life situations in computer code.
Scenario
The
break
statement is used to exit/terminate a loop.
Design a program that uses a
while
loop and continuously asks the user to enter a word unless the user enters "chupacabra"
as the secret exit word, in which case the message "You've successfully left the loop."
should be printed to the screen, and the loop should terminate.
Don't print any of the words entered by the user. Use the concept of conditional execution and the
break
statement.
while True:
ReplyDeletesecret_word = input("Enter a secret word: ")
if secret_word == "chupacabra":
break
while True:
ReplyDeletesecret_word = input("Enter a secret word: ")
if secret_word == "chupacabra":
break
print("You've successfully left the loop.")