How to Get Get Boolean Input from Users in Python
This guide explains how to get boolean (True
/False
) input from users in Python. We'll cover simple string comparisons, handling various "yes/no" type inputs (case-insensitively), and using a while
loop for validation.
Basic Boolean Input (String Comparison)
The simplest way to get boolean input is to treat the user's input as a string and compare it to expected values (e.g., "True" or "False"):
user_input = input('Subscribe to newsletter? True / False: ')
if user_input == 'True':
print('The user typed in True')
elif user_input == 'False':
print('The user typed in False')
else:
print('Enter True or False')
- The input is always a string, so even if the user types
True
it is not equal to the boolean valueTrue
and must be compared with a string that has the valueTrue
. - The
input
returns a string, which can be compared to another string. - The check is case-sensitive.
Case-Insensitive Boolean Input
To make the input case-insensitive, use .lower()
or .capitalize()
to convert the input to a consistent case before comparison:
user_input = input('Subscribe to newsletter? True / False: ')
if user_input.lower() == 'true': # Convert to lowercase
print('The user typed in True')
elif user_input.lower() == 'false':
print('The user typed in False')
else:
print('Enter True or False')
user_input.lower()
: Converts the input to lowercase. Now "True", "true", "TRUE", etc., will all be treated the same. You could also use.upper()
and compare to'TRUE'
..capitalize()
can also be used, but it only capitalizes the first character and converts the rest to lowercase.
Accepting Multiple "Yes/No" Variations
To be more user-friendly, accept various ways of saying "yes" or "no" (e.g., "y", "yes", "n", "no"):
user_input = input('Do you like pizza (yes/no): ')
yes_choices = ['yes', 'y', 'true'] # Add more variations if needed
no_choices = ['no', 'n', 'false']
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_choices
andno_choices
: Lists of acceptable input strings.in
: Checks if the (lowercased) input is present in the respective list. This is much cleaner than multipleor
conditions.
Using a while
Loop for Input Validation
To ensure the user enters valid input, use a while
loop that repeats until a valid response is given:
yes_choices = ['yes', 'y', 'true']
no_choices = ['no', 'n', 'false']
while True: # Loop indefinitely
user_input = input('Do you want to continue? yes/no: ').lower()
if user_input in yes_choices:
print('User typed yes')
break # Exit the loop
elif user_input in no_choices:
print('User typed no')
break # Exit the loop
else:
print('Type yes or no')
# continue is implicit here, could be omitted.
while True:
: This creates an infinite loop. We'll usebreak
to exit the loop when we get valid input..lower()
: Converts the input to lowercase inside the loop for case-insensitive comparison.break
: Exits the loop when a valid "yes" or "no" response is given.continue
(optional): You could usecontinue
in theelse
block to explicitly jump to the next iteration, but it's implicit here.