How to Code a Yes or No Question in Python v3.8?

Estimated read time 2 min read

To code a simple Yes or No question in Python 3.8, you can use a while loop and the input function. Here’s an example:

while True:
    answer = input("Do you want to continue? (yes/no): ")
    
    if answer.lower() == "yes":
        print("Great! Continuing...")
        break
    elif answer.lower() == "no":
        print("Exiting...")
        break
    else:
        print("Invalid input. Please enter 'yes' or 'no'.")

In this example, the program will repeatedly prompt the user with the question “Do you want to continue? (yes/no):”. The input function is used to get the user’s response, which is stored in the answer variable.

The program then checks the value of answer using an if-elif-else statement. If the user enters “yes” (case insensitive), the program prints “Great! Continuing…” and breaks out of the while loop to continue with the desired logic. If the user enters “no” (case insensitive), the program prints “Exiting…” and breaks out of the loop to terminate the program. If the user enters any other value, the program prints “Invalid input. Please enter ‘yes’ or ‘no’.” and continues the loop to prompt the user again.

You can modify the messages and actions within the if-elif-else block to suit your specific use case.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply