100 days of code: Day 10 - while loops

Last modified on 2020-06-18

Source: 100-days-of-code.

A while loop runs as long as a condition remains True. This program - parrot.py - will continue to prompt the user to enter text, and repeat it back, until the user enters ‘quit’ (executing the break command which exits the loop) to end program …

prompt = ("\nTell me something, and I will repeat it back to you."
            + "\nEnter 'quit' to end program: ")
au_revoir = "\n(O<\tAu revoir!\n(/)_"

while True:
    message = input(prompt)
    if message.lower() == 'quit':
        print(au_revoir)
        break
    else:
        print(message)

Output …

$ ./parrot.py 

Tell me something, and I will repeat it back to you.
Enter 'quit' to end program: Hello birdie!
Hello birdie!

Tell me something, and I will repeat it back to you.
Enter 'quit' to end program: Polly want a cracker?
Polly want a cracker?

Tell me something, and I will repeat it back to you.
Enter 'quit' to end program: Hmm ... this feels like a one-sided conversation ...
Hmm ... this feels like a one-sided conversation ...

Tell me something, and I will repeat it back to you.
Enter 'quit' to end program: QuIT

(O<     Au revoir!
(/)_

Happy hacking!