Summary

Resources

Notes\ Code

Our code is nearly finished, we just need the ability to check our answers, and to do this our QuizBrain class needs a new method called check_answer(), as well as a new attribute to hold the score.

image.png

image.png

We add score to our init real quick.

    def __init__(self, q_list):
        self.question_number = 0
        self.question_list = q_list
        self.score = 0

To check our user’s answer we need to make the input taking it, placed inside a variable. Easy enough:

    def next_question(self):
        current_question = self.question_list[self.question_number]
        self.question_number += 1
        user_answer = input(f"Q. {self.question_number}: {current_question.text} (True/False): ")

Now we need to create the check_answer function but we also need to call it inside of next_question and pass it the user_answer as well as the correct answer stored in our current_question variable. We can do this simply enough, and much like we got the text for the question in our input by calling the current_question variable and saying current_question.text, we can just say current_question.answer to pass that information along. Now next_question looks like this:

    def next_question(self):
        current_question = self.question_list[self.question_number]
        self.question_number += 1
        user_answer = input(f"Q. {self.question_number}: {current_question.text} (True/False): ")
        self.check_answer(user_answer, current_question.answer)

So lets write our check_answer method now:

    def check_answer(self, user_answer, correct_answer):
        if user_answer.lower() == correct_answer.lower():
            print("That is correct")
            self.score += 1
        else:
            print("That is incorrect")
        print(f"Your current score is {self.score}/{self.question_number}")