Solution: Break the Loop
Explore how to manage infinite loops in Python by using user input to break the loop. Understand the use of conditions to skip empty inputs and keep a conversation going until the user decides to stop by typing a specific word.
We'll cover the following...
This program keeps a conversation going until the user says "bye". It also ignores empty input.
while True:creates an infinite loop, meaning it keeps running until you tell it to stop.input()lets the user type something, which is stored in the variableword.if word == "bye":checks if the user typed"bye".If yes, the
breakstatement stops the loop.
if word == "":checks if the user just pressed Enter without typing anything.If yes,
continueskips the rest of the loop and starts over.
Otherwise, Python prints whatever the user said.
while True:
word = input("Say something: ")
if word == "bye":
break
if word == "":
continue
print("You said:", word)