Exit a while loop based on user Input in Python

avatar

Borislav Hadzhiev

Last updated: Aug 23, 2022

banner

Photo from Unsplash

Exit a while loop based on user Input in Python #

To exit a while loop based on user input:

  1. Use the input() function to take input from the user.
  2. On each iteration, check if the input value meets a condition.
  3. If the condition is met, use the break statement to exit the loop.
main.py
# ✅ Exit while loop when user enters 5 or more characters password = '' while True: password = input('Enter your password: ') if len(password) < 5: print('Password too short') continue else: print(f'You entered {password}') break print(password) # ---------------------------------------- # ✅ Exit while loop when user enters a valid integer num = 0 while True: try: num = int(input("Enter your favorite integer: ")) except ValueError: print("Please enter a valid integer") continue else: print(f'You entered: {num}') break print(num) if num > 100: print('The provided number is greater than 100') elif num == 100: print('The provided number is equal to 100') else: print('The provided number is less than 100')

The first example uses a while True loop and checks if the user entered a value containing 5 or more characters.

If the user enters 5 or more characters, we use the break statement to break out of the loop.

exit while loop with user input

main.py
password = '' while True: password = input('Enter your password: ') if len(password) < 5: print('Password too short') continue else: print(f'You entered {password}') break print(password)

If the user enters a value that has a length of less than 5, we use the continue statement to continue to the next iteration and prompt the user again.

If the user enters a value with a length of at least 5, we use the break statement to exit the while loop.

The continue statement continues with the next iteration of the loop.

The break statement breaks out of the innermost enclosing for or while loop.

When validating user input in a while loop, we use the continue statement when the input is invalid, e.g. in an except block or an if statement.

If the input is valid, we use the break statement to exit out of the while loop.

The following example exits a while loop when the user enters a valid integer.

main.py
num = 0 while True: try: num = int(input("Enter your favorite integer: ")) except ValueError: print("Please enter a valid integer") continue else: # 👇️ exit while loop when user enters valid integer print(f'You entered: {num}') break print(num)

exit while loop with user input try except

If the code in the try block raises a ValueError, the except block runs, where we use the continue statement to continue to the next iteration.

If the user enters a valid integer, the try block completes successfully and then the else block runs where we use the break statement to exit out of the while loop.

The input function takes an optional prompt argument and writes it to standard output without a trailing newline.

The function then reads the line from input, converts it to a string and returns the result.

Note that the input() function is always guaranteed to return a string, even if the user enters a number.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.