Last updated: Apr 9, 2024
Reading time·4 min
To ask the user a yes/no question:
input()
function to take input from the user.yes
or no
.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')
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.
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.
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')
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
.
To create a yes/no while loop with user input:
while
loop to iterate until a condition is met.input()
function to get input from the user.no
, use the break
statement to break out of the loop.user_input = '' while True: user_input = input('Do you want to continue? yes/no: ') if user_input.lower() == 'yes': print('User typed yes') continue elif user_input.lower() == 'no': print('User typed no') break else: print('Type yes/no')
We used a while True
loop to iterate until the user types no
.
if
statement checks if the user typed yes
and if the condition is met, it continues to the next iteration.The continue statement continues with the next iteration of the loop.
user_input = '' while True: user_input = input('Do you want to continue? yes/no: ') if user_input.lower() == 'yes': print('User typed yes') continue elif user_input.lower() == 'no': print('User typed no') break else: print('Type yes/no')
If the user types no
, we print a message and break out of the while True
loop.
The break statement breaks out of the innermost enclosing for or while loop.
The else
block runs when the user types anything else.
If you have multiple words that you consider to be yes
or no
, add the words
to a list and use the in
operator to check for membership.
yes_choices = ['yes', 'y'] no_choices = ['no', 'n'] while True: user_input = input('Do you want to continue? yes/no: ') if user_input.lower() in yes_choices: print('User typed yes') continue elif user_input.lower() in no_choices: print('User typed no') break else: print('Type yes/no')
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
.
You can also use a while
loop if you only want to allow the user to enter some
variation of yes
and no
.
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
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 enters an invalid value, the else
block runs, where we use the
continue
statement to prompt the user again.
You can learn more about the related topics by checking out the following tutorials:
__main__
module in Path