Skip to content

Python exit while loop with user input | Example code

  • by

The while statement has conditional expression, and the user enters the String. Just need to use user input in the while statement conditional and evaluate it. If false then Python exits while loop with user input.

Example exit while loop with user input in Python

Simple example code.

flag = "1"

while flag != "0":

    print("Not broken")
    flag = input("to break loop enter '0': ")


Python exit while loop with user input

Another example

Add the movie entered to a list. Continue asking for a movie until the user enters ‘0’. After all, movies have been input, output the list of movies one movie per line.

Use the break keyword to interrupt either a while or a for-loop.

def addMovie():
    movies = []
    while True:
        movie = input("Enter the name of a movie: ")
        if movie == "0":

    print("That's your list")



Enter the name of a movie: AAA
Enter the name of a movie: BBB
Enter the name of a movie: 0
That’s your list
[‘AAA’, ‘BBB’]


Do comment if you have any doubts or suggestions on this Python input function.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.