If/else statement based on user input in Python

avatar

Borislav Hadzhiev

Last updated: Aug 22, 2022

banner

Photo from Unsplash

If/else statement based on user input in Python #

Use the input() function to take user input which can be used in an if/else statement. The input() function takes a value from the user, converts it to a string and returns the result.

main.py
# ✅ If/else statement with user input string user_input = input('Do you like pizza (yes/no) ? ') if user_input.lower() == 'yes': print('User typed in yes') elif user_input.lower() == 'no': print('User typed in no') else: print('Answer with yes or no') # ------------------------------------------ # ✅ If/else statement with user input number user_input = input('Enter an integer: ') try: user_input = int(user_input) if user_input > 100: print('The number is greater than 100') else: print('The number is less than 100') except ValueError: print('The value is not a valid integer')
If you need to keep prompting the user until they enter a correct value using a while loop, scroll down to the next subheading.

The first example takes a string from user input and uses it in an if/else statement.

main.py
user_input = input('Do you like pizza (yes/no) ? ') if user_input.lower() == 'yes'.lower(): print('User typed in yes') elif user_input.lower() == 'no'.lower(): print('User typed in no') else: print('Answer with yes or no')

If the user enters yes, the if block runs. If the user enters no, the elif block runs.

If the user enters a different value, the else block runs.

The example above uses the str.lower() function to convert the input value to lowercase to perform a case-insensitive comparison.

The str.lower method returns a copy of the string with all the cased characters converted to lowercase.

If you need to take a number from user input, make sure to use the int() or float() classes to convert the string to an integer or a float before using it in an if/else statement.

main.py
user_input = input('Enter an integer: ') try: user_input = int(user_input) if user_input > 100: print('The number is greater than 100') else: print('The number is less than 100') except ValueError: print('The value is not a valid integer')

The input() function is guaranteed to return a string, so we used the int() class to convert the value to an integer.

If/else statement based on user input in a while loop #

Use a while loop to keep prompting the user to enter a value that is used in an in/else statement.

main.py
user_input = '' while user_input not in ['yes', 'no']: user_input = input('Do you like pizza (yes/no) ? ') if user_input == 'yes': print('User typed in yes') break elif user_input == 'no': print('User typed in no') break else: print('Answer with yes or no')

The while loop keeps iterating until the user_input variable gets set to yes or no.

If the variable is set to yes, the if block runs.

If it is set to no, the elif block runs.

If the input value is set to anything else, the else block runs and the while loop keeps iterating.

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

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.