WEBVTT

0
00:00.180 --> 00:02.490
Currently when we run our code

1
00:02.910 --> 00:06.780
you can see that while we can input our answer,

2
00:07.380 --> 00:12.030
our code doesn't actually really care because it's not checking to see if it's

3
00:12.030 --> 00:14.280
right or wrong. In this lesson

4
00:14.310 --> 00:17.520
we're going to add one more method to our QuizBrain

5
00:17.970 --> 00:22.970
and that's the ability to check the answer that the user inputted and see if it

6
00:22.980 --> 00:27.870
was correct. And that means we need to keep track of the user's score.

7
00:27.990 --> 00:30.300
So we'll add another attribute score

8
00:30.540 --> 00:33.030
which has a default value of zero to begin with

9
00:33.360 --> 00:38.220
and it'll increase every time they get a question right. So in this lesson,

10
00:38.250 --> 00:42.660
we're going to create a new method called check_answer.

11
00:43.470 --> 00:48.470
And this check_answer function is going to be used to see if the user's answer

12
00:50.640 --> 00:55.640
which comes from this input is the same as the actual answer to the current

13
00:57.240 --> 00:58.073
question.

14
00:58.920 --> 01:03.920
What we need to do is we need to save this user's input inside a variable

15
01:05.190 --> 01:07.350
which we'll call user_answer.

16
01:08.430 --> 01:12.150
And then I'm going to call our check_answer method

17
01:12.600 --> 01:17.100
and I want to pass over the user_answer. Now, in addition,

18
01:17.100 --> 01:22.100
I also want to pass over the correct answer and the correct answer is going to

19
01:22.830 --> 01:26.130
be the current_question.answer.

20
01:26.430 --> 01:30.330
So let's replace that with current_question.answer

21
01:30.990 --> 01:35.190
because we know that current_question is a question object

22
01:35.490 --> 01:38.820
which has a text attribute and an answer attribute.

23
01:39.480 --> 01:44.480
And once we've passed over the user's answer and the correct answer,

24
01:45.180 --> 01:49.440
then we can receive it inside our check_answer function as a parameter.

25
01:50.580 --> 01:52.770
So we'll call this user_answer

26
01:53.340 --> 01:56.760
and the second one we'll call the correct_answer.

27
01:58.560 --> 02:01.860
So now this input is going to be passed over here

28
02:02.310 --> 02:06.210
and the correct answer is going to be under this variable name.

29
02:06.780 --> 02:11.780
So now we can see if the user_answer is equal to the correct_answer.

30
02:13.260 --> 02:14.790
And just for safety sake,

31
02:14.820 --> 02:18.840
it's usually a good idea to drop both sides to

32
02:18.840 --> 02:22.050
a lowercase so they are comparable. This way

33
02:22.050 --> 02:25.620
just in case the user answers True or true,

34
02:25.890 --> 02:27.660
it will be treated exactly the same.

35
02:28.860 --> 02:32.940
If the user_answer matches the correct answer, well

36
02:32.940 --> 02:35.880
then this means that they got it right, so let's print it.

37
02:38.550 --> 02:42.960
But on the other hand, if that was false then that means they got it wrong.

38
02:43.080 --> 02:45.210
So let's tell them that's wrong.

39
02:46.500 --> 02:51.500
Now it might also be a good idea to tell the user what the actual correct answer

40
02:51.750 --> 02:56.010
was. Now you have a choice depending on where you want to put that.

41
02:56.070 --> 03:01.070
So you can either choose to only show them the correct answer if they got it

42
03:01.510 --> 03:03.340
wrong like this.

43
03:03.580 --> 03:06.670
But I think it's actually a good idea to show them the correct answer

44
03:06.970 --> 03:08.320
even if they're got it right.

45
03:08.470 --> 03:13.030
So I'm going to change the indentation so that it's actually outside of the

46
03:13.030 --> 03:13.990
if/else block,

47
03:14.500 --> 03:19.120
and it's just simply going to happen once the if/else block is complete.

48
03:19.930 --> 03:24.930
Now, the next thing to do is to keep track of the user's score so that every

49
03:25.780 --> 03:30.760
time they get a question right then it should increase the score by one.

50
03:31.450 --> 03:36.450
Pause the video and add a new attribute called score to the QuizBrain class and

51
03:37.510 --> 03:41.650
increment it by one every time the user gets it right. Now,

52
03:41.650 --> 03:46.510
what you're aiming for is to be able to print out the score and the current

53
03:46.510 --> 03:51.510
question so that you can tell the user 'Your current score is:' and printed out

54
03:52.330 --> 03:54.340
after every single question.

55
03:55.300 --> 03:57.730
Pause the video now and complete this challenge.

56
04:00.400 --> 04:04.750
All right. So let's go ahead and create our new score attribute

57
04:05.110 --> 04:08.440
which is again going to have a default starting value of zero.

58
04:09.070 --> 04:12.820
And then whenever the user gets a question right,

59
04:13.090 --> 04:16.300
then we're going to increase their score by one.

60
04:16.660 --> 04:20.290
So let's tap into that score attribute and then increase it by one.

61
04:21.250 --> 04:24.010
Now we've got our score being tracked,

62
04:24.070 --> 04:28.810
then we want to be able to print out what the users' current score is.

63
04:29.350 --> 04:34.090
So at the end of the check_answer, let's create another print statement

64
04:34.450 --> 04:36.640
which gives the user their score.

65
04:36.820 --> 04:39.820
So 'Your current score is:'

66
04:40.360 --> 04:43.360
and then we'll insert the self.score.

67
04:44.440 --> 04:49.090
And we'll also tell them out of a possible number of questions,

68
04:49.120 --> 04:52.240
so we can insert the self.question_number.

69
04:52.660 --> 04:56.380
So this means that they've completed five questions so far where they've had

70
04:56.380 --> 04:58.600
five chances to get things right

71
04:58.930 --> 05:03.040
then we can show them that they got however many right out of those five.

72
05:03.370 --> 05:07.510
So maybe they got 3/5 right, or maybe they got 5/5

73
05:07.510 --> 05:10.570
right. And now if we run our code,

74
05:10.630 --> 05:12.970
we can see that print statement in action.

75
05:15.400 --> 05:18.850
So it tells me that I got it right and then the--

76
05:19.930 --> 05:23.590
and then the correct answer was true.

77
05:24.070 --> 05:26.950
And that my current score is 1/1.

78
05:32.940 --> 05:33.773
<v 1>Right</v>

79
05:34.260 --> 05:36.780
<v 0>Now, if I get one of these wrong,</v>

80
05:37.350 --> 05:42.300
then you can see that my current score is now 3/4.

81
05:43.230 --> 05:46.800
And it tells the user that they've missed out on one point.

82
05:47.760 --> 05:51.510
It'd be nice to add a little bit of space in between each of the questions.

83
05:51.570 --> 05:55.110
I want to be able to see all the questions that I've done so far.

84
05:55.320 --> 05:56.910
So I don't want to clear the screen,

85
05:57.230 --> 06:00.020
but I want to be able to add a new line here.

86
06:00.560 --> 06:05.540
So at the end of my print statement, I'm going to add a print

87
06:05.570 --> 06:07.490
which is just going to print a new line.

88
06:08.780 --> 06:10.670
So that in between each question

89
06:10.670 --> 06:14.930
I have a little bit of space to tell each question apart from the other.

90
06:16.010 --> 06:20.060
The final thing I need to do is to tell the user

91
06:20.330 --> 06:23.660
their final score once the entire quiz is finished,

92
06:24.170 --> 06:29.170
and we want to be able to print something like 'You've completed the quiz' and

93
06:32.450 --> 06:34.730
then we'll print out, um,

94
06:34.760 --> 06:38.210
something like 'Your final score was:'

95
06:38.390 --> 06:42.230
and then we'll give them their final score so maybe they got 10/12

96
06:42.620 --> 06:47.420
correct. And this way they can see what their final outcome was.

97
06:47.900 --> 06:51.740
So have a think about how you might be able to print these two lines here

98
06:52.190 --> 06:57.190
and how you can get hold of the question number and the user's score to print

99
06:57.800 --> 07:01.820
inside our main.py. So pause the video and complete this challenge.

100
07:03.620 --> 07:06.080
All right. So the first line is very easy to print.

101
07:06.140 --> 07:08.990
All we need to do is wrap a print statement around it.

102
07:09.560 --> 07:13.610
The second line is a little bit more tricky because it will need to have an 

103
07:13.610 --> 07:14.443
fstring.

104
07:14.450 --> 07:19.450
So we want this second value to be the total number of questions in the question

105
07:20.300 --> 07:21.133
bank,

106
07:21.350 --> 07:25.700
or it could be the current question number that the user is on.

107
07:26.270 --> 07:27.200
You can do either.

108
07:27.230 --> 07:32.230
You can either say question bank and wrap a length function around it,

109
07:34.100 --> 07:37.010
like this, which will give you the total number of questions

110
07:37.310 --> 07:40.880
cause we're at the end of the quiz now. Alternatively,

111
07:40.910 --> 07:45.910
you can also tap into the quiz object and get the question number when the quiz

112
07:46.910 --> 07:50.150
ended. And similarly with the score,

113
07:50.150 --> 07:52.370
you can say quiz.score.

114
07:53.120 --> 07:56.300
So now once the quiz has completed,

115
07:57.980 --> 08:01.700
you can see our print statements. You've completed the quiz and your final score

116
08:01.730 --> 08:06.500
was 0/12 because I was just button mashing and it didn't get any

117
08:06.500 --> 08:07.370
questions right.