Handling Yes/No User Input in Python

Introduction

In this Byte, we'll see how to handle user input in Python, specifically how to get a Yes/No answer. This kind of input is needed in quite a few applications, like command line utilities.

Link: This short Byte talks specifically about getting yes/no answers from users. If you want a more detailed guide on getting generic user input, see our article Getting User Input in Python
.

Requesting Yes/No User Input in Python

In Python, we can solicit Yes/No user input using the input() function. Here's a simple example:

user_input = input("Do you want to continue? (yes/no): ")
if user_input.lower() == "yes":
    print("Continuing...")
else:
    print("Exiting...")

When you run this code, it will prompt you with the question "Do you want to continue? (yes/no): ". If you enter "yes", it prints "Continuing...", otherwise it prints "Exiting...".

Handling Variations of Yes/No Responses

In a real-world scenario, users might not always respond with a simple "yes" or "no". They could respond with "y", "n", "YES", "NO", "Yes", "No", etc. To accommodate these variations, we can modify our code as follows:

user_input = input("Do you want to continue? (yes/no): ")
if user_input.lower() in ["yes", "y"]:
    print("Continuing...")
else:
    print("Exiting...")
Get free courses, guided projects, and more

No spam ever. Unsubscribe anytime. Read our Privacy Policy.

In this code, we convert the user's response to lowercase and then check if it's in the list ["yes", "y"]. This way, we accommodate multiple variations of "yes".

Note: This code still isn't perfect. It doesn't handle inputs like "YES ", " Y", etc. You might also want to use the strip() function to remove leading and trailing whitespaces.

Implementing Yes/No User Input within a While Loop

Sometimes, you might want to keep asking the user until they provide a valid response. You can achieve this using a while loop. Here's how:

while True:
    user_input = input("Do you want to continue? (yes/no): ")
    if user_input.lower() in ["yes", "y"]:
        print("Continuing...")
        break
    elif user_input.lower() in ["no", "n"]:
        print("Exiting...")
        break
    else:
        print("Invalid input. Please enter yes/no.")

In this code, we keep asking the user "Do you want to continue? (yes/no): " until they enter a valid response. If the response is "yes" or "y", we print "Continuing..." and break out of the loop. If the response is "no" or "n", we print "Exiting..." and break out of the loop. If the response is anything else, we print "Invalid input. Please enter yes/no." and continue with the next iteration of the loop.

Conclusion

In this Byte, we've explored how to solicit Yes/No user input in Python. We've also discussed how to accommodate variations of Yes/No responses and how to implement Yes/No user input within a while loop.

Last Updated: August 21st, 2023
Was this helpful?

© 2013-2024 Stack Abuse. All rights reserved.

AboutDisclosurePrivacyTerms