Last updated: Apr 9, 2024
Reading time·6 min

To validate user input:
while loop to iterate until the provided input value is valid.while loop.# ✅ Validating integer user input num = 0 while True: try: num = int(input("Enter an integer 1-10: ")) except ValueError: print("Please enter a valid integer 1-10") continue if num >= 1 and num <= 10: print(f'You entered: {num}') break else: print('The integer must be in the range 1-10') # ---------------------------------------------- # ✅ Validating string user input 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)

The first example validates numeric user input in a while loop.
If the try block completes successfully, then the user entered an integer.
num = 0 while True: try: num = int(input("Enter an integer 1-10: ")) except ValueError: print("Please enter a valid integer 1-10") continue if num >= 1 and num <= 10: print(f'You entered: {num}') break else: print('The integer must be in the range 1-10')
The if statement checks if the integer is in the range 1-10 and if the
condition is met, we break out of the while loop.
The break statement breaks out of the innermost enclosing for or while loop.
If the integer is not in the specified range, the else block runs and prints a
message.
If the user didn't enter an integer, the except block runs, where we use the
continue statement to
prompt the user again.
The continue statement continues with the next iteration of the loop.
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 input function
takes an optional prompt argument and writes it to standard output without a
trailing newline.
The function then reads the line from the input, converts it to a string and returns the result.
input() function is always guaranteed to return a string, even if the user enters a number.You can use the same approach when validating user input strings.
Here is an example that prompts the user for input until they enter a value that is at least 5 characters long.
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)

The while loop keeps iterating until the user enters a value that has a length
of at least 5.
continue statement to continue to the next iteration.If the value is at least 5 characters long, we use the break statement as the
input is valid.
You can use the boolean OR and boolean AND operators if you need to check for multiple conditions.
Here is an example that checks if the input value is at least 5 characters long and not in a list of values.
password = '' common_passwords = ['abcde', 'asdfg'] while True: password = input('Enter your password: ') if len(password) < 5 or password in common_passwords: print('Pick a strong password') continue else: print(f'You entered {password}') break print(password)

The if statement checks if the password is less than 5 characters or is in
the commonly used passwords list.
We used the boolean or operator, so the if block runs if either of the two
conditions is met.
5 characters or is contained in the commonly used passwords list, we continue to the next iteration and prompt the user again.Use the and boolean operator if you need to check if multiple conditions are
met when validating the input.
password = '' common_passwords = ['abcde', 'asdfg'] while True: password = input('Enter your password: ') if len(password) > 5 and password not in common_passwords: print(f'You entered {password}') break else: print('Pick a strong password') continue print(password)
We used the and boolean operator, so for the if block to run both conditions
have to be met.
The password has to be longer than 5 characters and it has to not be in the
commonly used passwords list.
If the conditions are met, we use the break statement to exit out of the
while True loop.
If the conditions aren't met, we use the continue statement to continue to the
next iteration.
To accept input until the Enter key is pressed:
while loop to iterate an arbitrary number of times.while loop when the user presses Enter.# ✅ When taking strings as input my_list = [] user_input = '' while True: user_input = input('Enter a string: ') # 👇️ Take input until Enter is pressed without value if user_input == '': print('User pressed enter') break my_list.append(user_input) print(my_list) # --------------------------------------------- # ✅ When taking integers as input my_list = [] user_input = '' while True: user_input = input('Enter a number: ') # 👇️ Take input until Enter is pressed without value if user_input == '': print('User pressed enter') break try: my_list.append(int(user_input)) except ValueError: print('Invalid number.') continue print(my_list)

Enter without typing in a value.This could be any other condition, e.g. if the user types done or if the list
stores at least 3 input values.
The first example takes strings as input from the user.
my_list = [] user_input = '' while True: user_input = input('Enter a string: ') # 👇️ Take input until Enter is pressed without value if user_input == '': print('User pressed enter') break my_list.append(user_input) print(my_list)
We used a while loop to take user input while iterating an arbitrary number of
times.
while True loop is to use a break statement or to raise an exception.If the user presses Enter without typing in a value, we break out of the while
loop.
The break statement breaks out of the
innermost enclosing for or while loop.
Otherwise, we append the input value to the list.
The list.append() method adds an item to the end of the list.
Here is an example that keeps repeating the program until the user input is correct when taking integers.
my_list = [] user_input = '' while True: user_input = input('Enter a number: ') # 👇️ Take input until Enter is pressed without value if user_input == '': print('User pressed enter') break try: my_list.append(int(user_input)) except ValueError: print('Invalid number.') continue print(my_list)

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 the input, converts it to a string and returns the result.
input() function is guaranteed to return a string, even if the user enters a number.We used the int() class to convert each string to an integer.
The try/except
statement is used to handle the ValueError that is raised if an invalid
integer is passed to the int() class.
You can learn more about the related topics by checking out the following tutorials: