WEBVTT

0
00:00.480 --> 00:03.360
Now that we've managed to create a question bank

1
00:03.420 --> 00:06.960
which consists of a list of question

2
00:06.990 --> 00:11.990
objects, the next natural thing to do is to actually bring up one of those

3
00:13.440 --> 00:18.440
questions and to ask the user to answer the question. Now,

4
00:19.380 --> 00:23.370
for all of the questioning and quizzing functionality,

5
00:23.760 --> 00:25.890
we're going to put that into the QuizBrain,

6
00:26.370 --> 00:28.800
which is going to be a class in its own right.

7
00:29.280 --> 00:33.570
And it's going to manage everything from asking the user for the next question,

8
00:33.870 --> 00:37.920
checking whether if the answer was right and also checking to see if we're

9
00:37.920 --> 00:39.660
actually at the end of the quiz.

10
00:40.530 --> 00:44.070
You're going to be creating this QuizBrain and class

11
00:44.550 --> 00:49.550
and this class is going to have two attributes to begin with; a question number

12
00:50.910 --> 00:53.760
which is going to have a default value of zero

13
00:54.090 --> 00:57.660
because all our quizzes will start from the first question.

14
00:58.290 --> 01:02.700
And this is going to keep track of which question the user is currently on.

15
01:03.210 --> 01:07.410
And we're going to use that number to go through the list of questions

16
01:07.770 --> 01:12.570
which will be passed over to this QuizBrain object when it gets initialized.

17
01:13.230 --> 01:17.100
And then you're going to have a method which is called next_question

18
01:17.490 --> 01:21.210
which will pull up the question from that list

19
01:21.450 --> 01:23.880
depending on which current question number we're on.

20
01:24.480 --> 01:28.080
If you take a look at the final version of the quiz game,

21
01:28.500 --> 01:33.500
you can see that it displays the question number, Q. and then that number.

22
01:34.050 --> 01:36.120
And then it displays the question text,

23
01:36.600 --> 01:39.900
and then it asks the user for an input, true or false.

24
01:40.860 --> 01:43.380
This is what we're going to be focusing on in this lesson.

25
01:44.130 --> 01:46.890
Inside your quiz-game-start,

26
01:47.160 --> 01:51.060
go to the quiz_brain file and create the quiz brain class.

27
01:51.600 --> 01:54.930
Add those two attributes; one is the question number

28
01:54.930 --> 01:59.310
which starts out with a default value and the other is the question list

29
01:59.520 --> 02:03.000
which is going to be initialized when you create a new quiz brain.

30
02:03.540 --> 02:08.540
And we're going to be passing over this question bank here into this question

31
02:08.940 --> 02:11.280
list when we initialize a new QuizBrain.

32
02:12.030 --> 02:15.300
Pause the video and see if you can complete this challenge.

33
02:15.380 --> 02:16.213
<v 1>Right?</v>

34
02:19.880 --> 02:23.030
<v 0>All right. So let's start off by creating our new class</v>

35
02:23.060 --> 02:28.060
which is going to be called QuizBrain and inside this class, we'll need to

36
02:28.790 --> 02:30.920
initialize some of those attributes,

37
02:31.280 --> 02:35.870
so I'm going to create the init method. Inside the init method

38
02:35.930 --> 02:40.700
I'm going to create that first attribute which was called question_number

39
02:41.570 --> 02:45.230
and I said we would set it to have a default value of zero.

40
02:45.380 --> 02:48.800
So this means every time we create a new QuizBrain 

41
02:48.800 --> 02:50.390
object from this class,

42
02:50.750 --> 02:55.190
it's ready going to have an attribute called question_number that set to zero.

43
02:56.030 --> 03:00.520
Now the next attribute is called question_list,

44
03:01.210 --> 03:05.230
and this is not going to have a default value. Instead,

45
03:05.290 --> 03:08.470
it's going to get it as a value passed over.

46
03:09.040 --> 03:14.040
So lets put that as a q_list here and set it equal to question_list.

47
03:15.490 --> 03:18.640
So what's going to happen is inside our main.py file,

48
03:18.910 --> 03:23.260
we're going to be initializing that QuizBrain and the value that we're going to

49
03:23.260 --> 03:28.260
put in there as the question list is going to be the question bank. This way

50
03:30.010 --> 03:34.810
we'll receive the question bank and it will be inside this question list

51
03:34.840 --> 03:38.170
attribute. Now I have another challenge for you.

52
03:38.560 --> 03:42.640
Create a method called next_question inside the QuizBrain.

53
03:43.360 --> 03:48.250
This method needs to retrieve the item at the current question number from the

54
03:48.250 --> 03:51.250
question list. And once you have this item,

55
03:51.310 --> 03:56.310
use the input function to show the user the question text and ask for the

56
03:56.440 --> 03:59.650
user's answer. Pause the video and give this a go.

57
04:04.930 --> 04:06.430
Alright, here's the solution.

58
04:06.880 --> 04:10.330
First we're gonna define a new method called the next_question,

59
04:11.470 --> 04:16.470
and this method is basically going to get hold of the current question and that

60
04:17.980 --> 04:21.280
is of course going to be from the question list.

61
04:21.730 --> 04:26.650
And then we're going to tap into that list and get hold of the item at the

62
04:26.860 --> 04:30.430
current question_number. So for example,

63
04:30.430 --> 04:32.920
question_number is going to start out being zero

64
04:33.460 --> 04:36.160
and so it's going to go into this list

65
04:36.220 --> 04:41.220
which is basically going to be our list of questions from our main.py,

66
04:41.830 --> 04:43.300
so our question bank.

67
04:43.870 --> 04:48.870
And remember, each of the items inside that list is a question object and

68
04:49.030 --> 04:52.900
question objects have text attributes and answer attributes.

69
04:53.440 --> 04:56.800
So in addition to getting hold of the current question,

70
04:57.310 --> 05:02.310
we can then get hold of the current question text by taking the current question

71
05:04.690 --> 05:09.610
and then just simply writing .text to get the value that's saved in that

72
05:09.610 --> 05:10.443
attribute.

73
05:11.290 --> 05:14.830
We're going to use an input to ask the user for

74
05:14.890 --> 05:17.200
what their answer is to this question.

75
05:17.560 --> 05:21.550
This is going to be an fstring because we're going to be inserting the current_

76
05:21.550 --> 05:25.960
question.text. And at the very beginning

77
05:26.470 --> 05:27.340
if you remember,

78
05:27.340 --> 05:31.660
we also have the Q. and then the number and then the colon.

79
05:32.260 --> 05:35.530
So that will be the self.question_number.

80
05:36.100 --> 05:39.490
And then we've got our colon and then it's going to be the question text.

81
05:39.880 --> 05:44.530
And then we're going to ask the user for a input; true or false.

82
05:45.880 --> 05:48.220
And that's it. That completes the challenge.

83
05:48.670 --> 05:53.650
Now all that's left is to show the very first question to the user and run our

84
05:53.650 --> 05:58.250
code. Let's go back to the main.py and import the QuizBrain class.

85
05:58.970 --> 06:03.970
So we're going to say from the quiz_brain file import the QuizBrain class.

86
06:05.840 --> 06:08.030
And once we've created our question bank,

87
06:08.390 --> 06:11.810
then the next thing to do is to create our new quiz.

88
06:12.290 --> 06:15.380
So this is going to be a new QuizBrain object

89
06:15.980 --> 06:20.090
and when we initialize it, we have to pass in a list of questions.

90
06:20.510 --> 06:23.450
And that is of course going to be the question bank.

91
06:24.290 --> 06:27.050
So now that we've got our quiz up and running,

92
06:27.110 --> 06:31.460
then we can say quiz.next_question.

93
06:32.090 --> 06:35.000
Let's go ahead and run this main.py file.

94
06:35.660 --> 06:40.660
And you can see we get our question text being displayed, true or false being

95
06:41.240 --> 06:45.110
asked, but our question number starts from zero.

96
06:45.770 --> 06:46.790
And that is, of course,

97
06:46.790 --> 06:51.790
because we need zero here in order to get hold of the first question from the

98
06:52.520 --> 06:56.330
list and that's why this has a default value of zero.

99
06:56.840 --> 06:59.030
But by the time we reach this input,

100
06:59.120 --> 07:03.080
it would be really good if this actually read one instead of zero.

101
07:03.620 --> 07:07.160
So one way we could do this is we could do this plus one,

102
07:07.610 --> 07:12.140
but remembering that we actually need to increase the question number every

103
07:12.140 --> 07:14.570
single time we call next_question anyways,

104
07:14.930 --> 07:17.870
then once we've gotten hold of the current question,

105
07:18.170 --> 07:23.170
we can actually tap into that self.question_number and increase it by one.

106
07:23.900 --> 07:25.610
This will have the same effect,

107
07:25.760 --> 07:30.320
and if you look at it now we've got the right question number being shown here,

108
07:30.680 --> 07:35.570
the right text being shown, and then we can insert true or false.

109
07:36.680 --> 07:37.760
Now at this stage,

110
07:37.790 --> 07:42.020
our program is just going to exit. In the next lesson,

111
07:42.260 --> 07:47.260
we're going to have to work out a way for our questions to keep being asked. And

112
07:48.080 --> 07:50.840
we'll need to write some more code for that to work.