Yes/No question with user input in Python

avatar

Borislav Hadzhiev

Last updated: Aug 25, 2022

banner

Photo from Unsplash

Yes/No question with user input in Python #

To ask the user a yes/no question:

  1. Use the input() function to take input from the user.
  2. Use conditional statements to check if the user entered yes or no.
  3. Perform an action if either condition is met.
main.py
user_input = input('Do you like pizza (yes/no): ') if user_input.lower() == 'yes': print('user typed yes') elif user_input.lower() == 'no': print('user typed no') else: print('Type yes or no')

yes no question user input

We used the input() function to take input from the user.

The if statement checks if the user entered yes and prints a message.

We used the str.lower() method to convert the user input string to lowercase to perform a case-insensitive equality comparison.

main.py
print('YES'.lower()) # 👉️ 'yes' print('Yes'.lower()) # 👉️ 'yes'

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

The else block runs if the user typed something else.

You might also have multiple words that you consider to be yes or no.

If that's the case, add the words to a list and use the in operator to check for membership.

main.py
user_input = input('Do you like pizza (yes/no): ') yes_choices = ['yes', 'y'] no_choices = ['no', 'n'] if user_input.lower() in yes_choices: print('user typed yes') elif user_input.lower() in no_choices: print('user typed no') else: print('Type yes or no')

yes no question multiple choices

We used the in operator to check if the input value is either of the items in the list.

The in operator tests for membership. For example, x in l evaluates to True if x is a member of l, otherwise it evaluates to False.

If you only want to allow the user to enter some variation of yes and no, use a while loop.

main.py
yes_choices = ['yes', 'y'] no_choices = ['no', 'n'] while True: user_input = input('Do you like pizza (yes/no): ') if user_input.lower() in yes_choices: print('user typed yes') break elif user_input.lower() in no_choices: print('user typed no') break else: print('Type yes or no') continue

only allow user input yes no

We used a while loop to only allow the user to answer yes, y, no or n.

If the if block runs, we print a message and use the break statement to exit out of the loop.

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

If the user entered an invalid value, the else block runs, where we use the continue statement to prompt the user again.

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

When validating user input in a while loop, we use the continue statement when the input is invalid.

If the input is valid, we use the break statement to exit out of the 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.