Currently when we run our code
you can see that while we can input our answer,
our code doesn't actually really care because it's not checking to see if it's
right or wrong. In this lesson
we're going to add one more method to our QuizBrain
and that's the ability to check the answer that the user inputted and see if it
was correct. And that means we need to keep track of the user's score.
So we'll add another attribute score
which has a default value of zero to begin with
and it'll increase every time they get a question right. So in this lesson,
we're going to create a new method called check_answer.
And this check_answer function is going to be used to see if the user's answer
which comes from this input is the same as the actual answer to the current
question.
What we need to do is we need to save this user's input inside a variable
which we'll call user_answer.
And then I'm going to call our check_answer method
and I want to pass over the user_answer. Now, in addition,
I also want to pass over the correct answer and the correct answer is going to
be the current_question.answer.
So let's replace that with current_question.answer
because we know that current_question is a question object
which has a text attribute and an answer attribute.
And once we've passed over the user's answer and the correct answer,
then we can receive it inside our check_answer function as a parameter.
So we'll call this user_answer
and the second one we'll call the correct_answer.
So now this input is going to be passed over here
and the correct answer is going to be under this variable name.
So now we can see if the user_answer is equal to the correct_answer.
And just for safety sake,
it's usually a good idea to drop both sides to
a lowercase so they are comparable. This way
just in case the user answers True or true,
it will be treated exactly the same.
If the user_answer matches the correct answer, well
then this means that they got it right, so let's print it.
But on the other hand, if that was false then that means they got it wrong.
So let's tell them that's wrong.
Now it might also be a good idea to tell the user what the actual correct answer
was. Now you have a choice depending on where you want to put that.
So you can either choose to only show them the correct answer if they got it
wrong like this.
But I think it's actually a good idea to show them the correct answer
even if they're got it right.
So I'm going to change the indentation so that it's actually outside of the
if/else block,
and it's just simply going to happen once the if/else block is complete.
Now, the next thing to do is to keep track of the user's score so that every
time they get a question right then it should increase the score by one.
Pause the video and add a new attribute called score to the QuizBrain class and
increment it by one every time the user gets it right. Now,
what you're aiming for is to be able to print out the score and the current
question so that you can tell the user 'Your current score is:' and printed out
after every single question.
Pause the video now and complete this challenge.
All right. So let's go ahead and create our new score attribute
which is again going to have a default starting value of zero.
And then whenever the user gets a question right,
then we're going to increase their score by one.
So let's tap into that score attribute and then increase it by one.
Now we've got our score being tracked,
then we want to be able to print out what the users' current score is.
So at the end of the check_answer, let's create another print statement
which gives the user their score.
So 'Your current score is:'
and then we'll insert the self.score.
And we'll also tell them out of a possible number of questions,
so we can insert the self.question_number.
So this means that they've completed five questions so far where they've had
five chances to get things right
then we can show them that they got however many right out of those five.
So maybe they got 3/5 right, or maybe they got 5/5
right. And now if we run our code,
we can see that print statement in action.
So it tells me that I got it right and then the--
and then the correct answer was true.
And that my current score is 1/1.
Right
Now, if I get one of these wrong,
then you can see that my current score is now 3/4.
And it tells the user that they've missed out on one point.
It'd be nice to add a little bit of space in between each of the questions.
I want to be able to see all the questions that I've done so far.
So I don't want to clear the screen,
but I want to be able to add a new line here.
So at the end of my print statement, I'm going to add a print
which is just going to print a new line.
So that in between each question
I have a little bit of space to tell each question apart from the other.
The final thing I need to do is to tell the user
their final score once the entire quiz is finished,
and we want to be able to print something like 'You've completed the quiz' and
then we'll print out, um,
something like 'Your final score was:'
and then we'll give them their final score so maybe they got 10/12
correct. And this way they can see what their final outcome was.
So have a think about how you might be able to print these two lines here
and how you can get hold of the question number and the user's score to print
inside our main.py. So pause the video and complete this challenge.
All right. So the first line is very easy to print.
All we need to do is wrap a print statement around it.
The second line is a little bit more tricky because it will need to have an
fstring.
So we want this second value to be the total number of questions in the question
bank,
or it could be the current question number that the user is on.
You can do either.
You can either say question bank and wrap a length function around it,
like this, which will give you the total number of questions
cause we're at the end of the quiz now. Alternatively,
you can also tap into the quiz object and get the question number when the quiz
ended. And similarly with the score,
you can say quiz.score.
So now once the quiz has completed,
you can see our print statements. You've completed the quiz and your final score
was 0/12 because I was just button mashing and it didn't get any
questions right.