In this tutorial, we will learn how to check if an answer is correct in Python. This is a helpful skill to have, especially if you’re creating quizzes, tests, or any sort of program that involves checking user input against a stored answer. Let’s dive into the steps.
Step 1: Create a function to compare answers
First, we need to create a function that takes two arguments: the correct answer and the user’s answer. The function will then compare these two arguments and return either True or False depending on whether the answers match.
1 2 3 4 5 |
def is_answer_correct(correct_answer, user_answer): if correct_answer == user_answer: return True else: return False |
This simple function checks if the two arguments are equal, and returns True if they are, and False if they aren’t.
Step 2: Test the function with user input
To test our function, we need to provide it with a correct answer and a user’s answer. For this example, let’s assume we have a simple math question: What is 9 + 5? The correct answer is 14.
We can ask the user for their answer using the input() function and then pass both the correct answer and the user’s input to our function.
1 2 3 4 5 6 7 |
correct_answer = 14 user_answer = int(input("What is 9 + 5? ")) if is_answer_correct(correct_answer, user_answer): print("Correct!") else: print("Incorrect!") |
The input() function collects user input and returns it as a string. We need to convert the user’s answer to an integer using int() since we are working with numbers in this example.
Step 3: Handle different input types (Optional)
In its current form, our function can only check answers that are integers. If we want the function to work with different data types like strings or floats, we can use the isinstance() function to check the type of the input and handle them accordingly.
1 2 3 4 5 6 7 8 9 |
def is_answer_correct_v2(correct_answer, user_answer): if isinstance(correct_answer, str): if correct_answer.lower() == user_answer.lower(): return True elif isinstance(correct_answer, (int, float)): if correct_answer == float(user_answer): return True return False |
Now, our function can compare both string and numeric answers. For strings, we use the lower() method to make the comparison case-insensitive. For numeric answers, we convert them to floats before making the comparison, since when we want to support decimals, all numbers should be cast to float.
Full code
Here’s the full code for our initial version and the improved version of our function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
def is_answer_correct(correct_answer, user_answer): if correct_answer == user_answer: return True else: return False def is_answer_correct_v2(correct_answer, user_answer): if isinstance(correct_answer, str): if correct_answer.lower() == user_answer.lower(): return True elif isinstance(correct_answer, (int, float)): if correct_answer == float(user_answer): return True return False correct_answer = 14 user_answer = int(input("What is 9 + 5? ")) if is_answer_correct(correct_answer, user_answer): print("Correct!") else: print("Incorrect!") correct_answer_v2 = "Python" user_answer_v2 = input("What's the name of this programming language? ") if is_answer_correct_v2(correct_answer_v2, user_answer_v2): print("Correct!") else: print("Incorrect!") |
Conclusion
In this tutorial, we have learned how to create a function to check if an answer is correct in Python. This can be helpful when building quizzes, tests, and any other application that requires comparing user input to a correct answer. We have also seen how to handle different input types like integers, floats, and strings, making our function more versatile.