1
00:00:00,180 --> 00:00:02,490
Currently when we run our code

2
00:00:02,910 --> 00:00:06,780
you can see that while we can input our answer,

3
00:00:07,380 --> 00:00:12,030
our code doesn't actually really care because it's not checking to see if it's

4
00:00:12,030 --> 00:00:14,280
right or wrong. In this lesson

5
00:00:14,310 --> 00:00:17,520
we're going to add one more method to our QuizBrain

6
00:00:17,970 --> 00:00:22,970
and that's the ability to check the answer that the user inputted and see if it

7
00:00:22,980 --> 00:00:27,870
was correct. And that means we need to keep track of the user's score.

8
00:00:27,990 --> 00:00:30,300
So we'll add another attribute score

9
00:00:30,540 --> 00:00:33,030
which has a default value of zero to begin with

10
00:00:33,360 --> 00:00:38,220
and it'll increase every time they get a question right. So in this lesson,

11
00:00:38,250 --> 00:00:42,660
we're going to create a new method called check_answer.

12
00:00:43,470 --> 00:00:48,470
And this check_answer function is going to be used to see if the user's answer

13
00:00:50,640 --> 00:00:55,640
which comes from this input is the same as the actual answer to the current

14
00:00:57,240 --> 00:00:58,073
question.

15
00:00:58,920 --> 00:01:03,920
What we need to do is we need to save this user's input inside a variable

16
00:01:05,190 --> 00:01:07,350
which we'll call user_answer.

17
00:01:08,430 --> 00:01:12,150
And then I'm going to call our check_answer method

18
00:01:12,600 --> 00:01:17,100
and I want to pass over the user_answer. Now, in addition,

19
00:01:17,100 --> 00:01:22,100
I also want to pass over the correct answer and the correct answer is going to

20
00:01:22,830 --> 00:01:26,130
be the current_question.answer.

21
00:01:26,430 --> 00:01:30,330
So let's replace that with current_question.answer

22
00:01:30,990 --> 00:01:35,190
because we know that current_question is a question object

23
00:01:35,490 --> 00:01:38,820
which has a text attribute and an answer attribute.

24
00:01:39,480 --> 00:01:44,480
And once we've passed over the user's answer and the correct answer,

25
00:01:45,180 --> 00:01:49,440
then we can receive it inside our check_answer function as a parameter.

26
00:01:50,580 --> 00:01:52,770
So we'll call this user_answer

27
00:01:53,340 --> 00:01:56,760
and the second one we'll call the correct_answer.

28
00:01:58,560 --> 00:02:01,860
So now this input is going to be passed over here

29
00:02:02,310 --> 00:02:06,210
and the correct answer is going to be under this variable name.

30
00:02:06,780 --> 00:02:11,780
So now we can see if the user_answer is equal to the correct_answer.

31
00:02:13,260 --> 00:02:14,790
And just for safety sake,

32
00:02:14,820 --> 00:02:18,840
it's usually a good idea to drop both sides to

33
00:02:18,840 --> 00:02:22,050
a lowercase so they are comparable. This way

34
00:02:22,050 --> 00:02:25,620
just in case the user answers True or true,

35
00:02:25,890 --> 00:02:27,660
it will be treated exactly the same.

36
00:02:28,860 --> 00:02:32,940
If the user_answer matches the correct answer, well

37
00:02:32,940 --> 00:02:35,880
then this means that they got it right, so let's print it.

38
00:02:38,550 --> 00:02:42,960
But on the other hand, if that was false then that means they got it wrong.

39
00:02:43,080 --> 00:02:45,210
So let's tell them that's wrong.

40
00:02:46,500 --> 00:02:51,500
Now it might also be a good idea to tell the user what the actual correct answer

41
00:02:51,750 --> 00:02:56,010
was. Now you have a choice depending on where you want to put that.

42
00:02:56,070 --> 00:03:01,070
So you can either choose to only show them the correct answer if they got it

43
00:03:01,510 --> 00:03:03,340
wrong like this.

44
00:03:03,580 --> 00:03:06,670
But I think it's actually a good idea to show them the correct answer

45
00:03:06,970 --> 00:03:08,320
even if they're got it right.

46
00:03:08,470 --> 00:03:13,030
So I'm going to change the indentation so that it's actually outside of the

47
00:03:13,030 --> 00:03:13,990
if/else block,

48
00:03:14,500 --> 00:03:19,120
and it's just simply going to happen once the if/else block is complete.

49
00:03:19,930 --> 00:03:24,930
Now, the next thing to do is to keep track of the user's score so that every

50
00:03:25,780 --> 00:03:30,760
time they get a question right then it should increase the score by one.

51
00:03:31,450 --> 00:03:36,450
Pause the video and add a new attribute called score to the QuizBrain class and

52
00:03:37,510 --> 00:03:41,650
increment it by one every time the user gets it right. Now,

53
00:03:41,650 --> 00:03:46,510
what you're aiming for is to be able to print out the score and the current

54
00:03:46,510 --> 00:03:51,510
question so that you can tell the user 'Your current score is:' and printed out

55
00:03:52,330 --> 00:03:54,340
after every single question.

56
00:03:55,300 --> 00:03:57,730
Pause the video now and complete this challenge.

57
00:04:00,400 --> 00:04:04,750
All right. So let's go ahead and create our new score attribute

58
00:04:05,110 --> 00:04:08,440
which is again going to have a default starting value of zero.

59
00:04:09,070 --> 00:04:12,820
And then whenever the user gets a question right,

60
00:04:13,090 --> 00:04:16,300
then we're going to increase their score by one.

61
00:04:16,660 --> 00:04:20,290
So let's tap into that score attribute and then increase it by one.

62
00:04:21,250 --> 00:04:24,010
Now we've got our score being tracked,

63
00:04:24,070 --> 00:04:28,810
then we want to be able to print out what the users' current score is.

64
00:04:29,350 --> 00:04:34,090
So at the end of the check_answer, let's create another print statement

65
00:04:34,450 --> 00:04:36,640
which gives the user their score.

66
00:04:36,820 --> 00:04:39,820
So 'Your current score is:'

67
00:04:40,360 --> 00:04:43,360
and then we'll insert the self.score.

68
00:04:44,440 --> 00:04:49,090
And we'll also tell them out of a possible number of questions,

69
00:04:49,120 --> 00:04:52,240
so we can insert the self.question_number.

70
00:04:52,660 --> 00:04:56,380
So this means that they've completed five questions so far where they've had

71
00:04:56,380 --> 00:04:58,600
five chances to get things right

72
00:04:58,930 --> 00:05:03,040
then we can show them that they got however many right out of those five.

73
00:05:03,370 --> 00:05:07,510
So maybe they got 3/5 right, or maybe they got 5/5

74
00:05:07,510 --> 00:05:10,570
right. And now if we run our code,

75
00:05:10,630 --> 00:05:12,970
we can see that print statement in action.

76
00:05:15,400 --> 00:05:18,850
So it tells me that I got it right and then the--

77
00:05:19,930 --> 00:05:23,590
and then the correct answer was true.

78
00:05:24,070 --> 00:05:26,950
And that my current score is 1/1.

79
00:05:32,940 --> 00:05:33,773
Right

80
00:05:34,260 --> 00:05:36,780
Now, if I get one of these wrong,

81
00:05:37,350 --> 00:05:42,300
then you can see that my current score is now 3/4.

82
00:05:43,230 --> 00:05:46,800
And it tells the user that they've missed out on one point.

83
00:05:47,760 --> 00:05:51,510
It'd be nice to add a little bit of space in between each of the questions.

84
00:05:51,570 --> 00:05:55,110
I want to be able to see all the questions that I've done so far.

85
00:05:55,320 --> 00:05:56,910
So I don't want to clear the screen,

86
00:05:57,230 --> 00:06:00,020
but I want to be able to add a new line here.

87
00:06:00,560 --> 00:06:05,540
So at the end of my print statement, I'm going to add a print

88
00:06:05,570 --> 00:06:07,490
which is just going to print a new line.

89
00:06:08,780 --> 00:06:10,670
So that in between each question

90
00:06:10,670 --> 00:06:14,930
I have a little bit of space to tell each question apart from the other.

91
00:06:16,010 --> 00:06:20,060
The final thing I need to do is to tell the user

92
00:06:20,330 --> 00:06:23,660
their final score once the entire quiz is finished,

93
00:06:24,170 --> 00:06:29,170
and we want to be able to print something like 'You've completed the quiz' and

94
00:06:32,450 --> 00:06:34,730
then we'll print out, um,

95
00:06:34,760 --> 00:06:38,210
something like 'Your final score was:'

96
00:06:38,390 --> 00:06:42,230
and then we'll give them their final score so maybe they got 10/12

97
00:06:42,620 --> 00:06:47,420
correct. And this way they can see what their final outcome was.

98
00:06:47,900 --> 00:06:51,740
So have a think about how you might be able to print these two lines here

99
00:06:52,190 --> 00:06:57,190
and how you can get hold of the question number and the user's score to print

100
00:06:57,800 --> 00:07:01,820
inside our main.py. So pause the video and complete this challenge.

101
00:07:03,620 --> 00:07:06,080
All right. So the first line is very easy to print.

102
00:07:06,140 --> 00:07:08,990
All we need to do is wrap a print statement around it.

103
00:07:09,560 --> 00:07:13,610
The second line is a little bit more tricky because it will need to have an 

104
00:07:13,610 --> 00:07:14,443
fstring.

105
00:07:14,450 --> 00:07:19,450
So we want this second value to be the total number of questions in the question

106
00:07:20,300 --> 00:07:21,133
bank,

107
00:07:21,350 --> 00:07:25,700
or it could be the current question number that the user is on.

108
00:07:26,270 --> 00:07:27,200
You can do either.

109
00:07:27,230 --> 00:07:32,230
You can either say question bank and wrap a length function around it,

110
00:07:34,100 --> 00:07:37,010
like this, which will give you the total number of questions

111
00:07:37,310 --> 00:07:40,880
cause we're at the end of the quiz now. Alternatively,

112
00:07:40,910 --> 00:07:45,910
you can also tap into the quiz object and get the question number when the quiz

113
00:07:46,910 --> 00:07:50,150
ended. And similarly with the score,

114
00:07:50,150 --> 00:07:52,370
you can say quiz.score.

115
00:07:53,120 --> 00:07:56,300
So now once the quiz has completed,

116
00:07:57,980 --> 00:08:01,700
you can see our print statements. You've completed the quiz and your final score

117
00:08:01,730 --> 00:08:06,500
was 0/12 because I was just button mashing and it didn't get any

118
00:08:06,500 --> 00:08:07,370
questions right.

