1
00:00:00,480 --> 00:00:03,360
Now that we've managed to create a question bank

2
00:00:03,420 --> 00:00:06,960
which consists of a list of question

3
00:00:06,990 --> 00:00:11,990
objects, the next natural thing to do is to actually bring up one of those

4
00:00:13,440 --> 00:00:18,440
questions and to ask the user to answer the question. Now,

5
00:00:19,380 --> 00:00:23,370
for all of the questioning and quizzing functionality,

6
00:00:23,760 --> 00:00:25,890
we're going to put that into the QuizBrain,

7
00:00:26,370 --> 00:00:28,800
which is going to be a class in its own right.

8
00:00:29,280 --> 00:00:33,570
And it's going to manage everything from asking the user for the next question,

9
00:00:33,870 --> 00:00:37,920
checking whether if the answer was right and also checking to see if we're

10
00:00:37,920 --> 00:00:39,660
actually at the end of the quiz.

11
00:00:40,530 --> 00:00:44,070
You're going to be creating this QuizBrain and class

12
00:00:44,550 --> 00:00:49,550
and this class is going to have two attributes to begin with; a question number

13
00:00:50,910 --> 00:00:53,760
which is going to have a default value of zero

14
00:00:54,090 --> 00:00:57,660
because all our quizzes will start from the first question.

15
00:00:58,290 --> 00:01:02,700
And this is going to keep track of which question the user is currently on.

16
00:01:03,210 --> 00:01:07,410
And we're going to use that number to go through the list of questions

17
00:01:07,770 --> 00:01:12,570
which will be passed over to this QuizBrain object when it gets initialized.

18
00:01:13,230 --> 00:01:17,100
And then you're going to have a method which is called next_question

19
00:01:17,490 --> 00:01:21,210
which will pull up the question from that list

20
00:01:21,450 --> 00:01:23,880
depending on which current question number we're on.

21
00:01:24,480 --> 00:01:28,080
If you take a look at the final version of the quiz game,

22
00:01:28,500 --> 00:01:33,500
you can see that it displays the question number, Q. and then that number.

23
00:01:34,050 --> 00:01:36,120
And then it displays the question text,

24
00:01:36,600 --> 00:01:39,900
and then it asks the user for an input, true or false.

25
00:01:40,860 --> 00:01:43,380
This is what we're going to be focusing on in this lesson.

26
00:01:44,130 --> 00:01:46,890
Inside your quiz-game-start,

27
00:01:47,160 --> 00:01:51,060
go to the quiz_brain file and create the quiz brain class.

28
00:01:51,600 --> 00:01:54,930
Add those two attributes; one is the question number

29
00:01:54,930 --> 00:01:59,310
which starts out with a default value and the other is the question list

30
00:01:59,520 --> 00:02:03,000
which is going to be initialized when you create a new quiz brain.

31
00:02:03,540 --> 00:02:08,539
And we're going to be passing over this question bank here into this question

32
00:02:08,940 --> 00:02:11,280
list when we initialize a new QuizBrain.

33
00:02:12,030 --> 00:02:15,300
Pause the video and see if you can complete this challenge.

34
00:02:15,380 --> 00:02:16,213
Right?

35
00:02:19,880 --> 00:02:23,030
All right. So let's start off by creating our new class

36
00:02:23,060 --> 00:02:28,060
which is going to be called QuizBrain and inside this class, we'll need to

37
00:02:28,790 --> 00:02:30,920
initialize some of those attributes,

38
00:02:31,280 --> 00:02:35,870
so I'm going to create the init method. Inside the init method

39
00:02:35,930 --> 00:02:40,700
I'm going to create that first attribute which was called question_number

40
00:02:41,570 --> 00:02:45,230
and I said we would set it to have a default value of zero.

41
00:02:45,380 --> 00:02:48,800
So this means every time we create a new QuizBrain 

42
00:02:48,800 --> 00:02:50,390
object from this class,

43
00:02:50,750 --> 00:02:55,190
it's ready going to have an attribute called question_number that set to zero.

44
00:02:56,030 --> 00:03:00,520
Now the next attribute is called question_list,

45
00:03:01,210 --> 00:03:05,230
and this is not going to have a default value. Instead,

46
00:03:05,290 --> 00:03:08,470
it's going to get it as a value passed over.

47
00:03:09,040 --> 00:03:14,040
So lets put that as a q_list here and set it equal to question_list.

48
00:03:15,490 --> 00:03:18,640
So what's going to happen is inside our main.py file,

49
00:03:18,910 --> 00:03:23,260
we're going to be initializing that QuizBrain and the value that we're going to

50
00:03:23,260 --> 00:03:28,260
put in there as the question list is going to be the question bank. This way

51
00:03:30,010 --> 00:03:34,810
we'll receive the question bank and it will be inside this question list

52
00:03:34,840 --> 00:03:38,170
attribute. Now I have another challenge for you.

53
00:03:38,560 --> 00:03:42,640
Create a method called next_question inside the QuizBrain.

54
00:03:43,360 --> 00:03:48,250
This method needs to retrieve the item at the current question number from the

55
00:03:48,250 --> 00:03:51,250
question list. And once you have this item,

56
00:03:51,310 --> 00:03:56,310
use the input function to show the user the question text and ask for the

57
00:03:56,440 --> 00:03:59,650
user's answer. Pause the video and give this a go.

58
00:04:04,930 --> 00:04:06,430
Alright, here's the solution.

59
00:04:06,880 --> 00:04:10,330
First we're gonna define a new method called the next_question,

60
00:04:11,470 --> 00:04:16,470
and this method is basically going to get hold of the current question and that

61
00:04:17,980 --> 00:04:21,279
is of course going to be from the question list.

62
00:04:21,730 --> 00:04:26,650
And then we're going to tap into that list and get hold of the item at the

63
00:04:26,860 --> 00:04:30,430
current question_number. So for example,

64
00:04:30,430 --> 00:04:32,920
question_number is going to start out being zero

65
00:04:33,460 --> 00:04:36,160
and so it's going to go into this list

66
00:04:36,220 --> 00:04:41,220
which is basically going to be our list of questions from our main.py,

67
00:04:41,830 --> 00:04:43,300
so our question bank.

68
00:04:43,870 --> 00:04:48,870
And remember, each of the items inside that list is a question object and

69
00:04:49,030 --> 00:04:52,900
question objects have text attributes and answer attributes.

70
00:04:53,440 --> 00:04:56,800
So in addition to getting hold of the current question,

71
00:04:57,310 --> 00:05:02,310
we can then get hold of the current question text by taking the current question

72
00:05:04,690 --> 00:05:09,610
and then just simply writing .text to get the value that's saved in that

73
00:05:09,610 --> 00:05:10,443
attribute.

74
00:05:11,290 --> 00:05:14,830
We're going to use an input to ask the user for

75
00:05:14,890 --> 00:05:17,200
what their answer is to this question.

76
00:05:17,560 --> 00:05:21,550
This is going to be an fstring because we're going to be inserting the current_

77
00:05:21,550 --> 00:05:25,960
question.text. And at the very beginning

78
00:05:26,470 --> 00:05:27,340
if you remember,

79
00:05:27,340 --> 00:05:31,660
we also have the Q. and then the number and then the colon.

80
00:05:32,260 --> 00:05:35,530
So that will be the self.question_number.

81
00:05:36,100 --> 00:05:39,490
And then we've got our colon and then it's going to be the question text.

82
00:05:39,880 --> 00:05:44,530
And then we're going to ask the user for a input; true or false.

83
00:05:45,880 --> 00:05:48,220
And that's it. That completes the challenge.

84
00:05:48,670 --> 00:05:53,650
Now all that's left is to show the very first question to the user and run our

85
00:05:53,650 --> 00:05:58,250
code. Let's go back to the main.py and import the QuizBrain class.

86
00:05:58,970 --> 00:06:03,970
So we're going to say from the quiz_brain file import the QuizBrain class.

87
00:06:05,840 --> 00:06:08,030
And once we've created our question bank,

88
00:06:08,390 --> 00:06:11,810
then the next thing to do is to create our new quiz.

89
00:06:12,290 --> 00:06:15,380
So this is going to be a new QuizBrain object

90
00:06:15,980 --> 00:06:20,090
and when we initialize it, we have to pass in a list of questions.

91
00:06:20,510 --> 00:06:23,450
And that is of course going to be the question bank.

92
00:06:24,290 --> 00:06:27,050
So now that we've got our quiz up and running,

93
00:06:27,110 --> 00:06:31,460
then we can say quiz.next_question.

94
00:06:32,090 --> 00:06:35,000
Let's go ahead and run this main.py file.

95
00:06:35,660 --> 00:06:40,660
And you can see we get our question text being displayed, true or false being

96
00:06:41,240 --> 00:06:45,110
asked, but our question number starts from zero.

97
00:06:45,770 --> 00:06:46,790
And that is, of course,

98
00:06:46,790 --> 00:06:51,790
because we need zero here in order to get hold of the first question from the

99
00:06:52,520 --> 00:06:56,330
list and that's why this has a default value of zero.

100
00:06:56,840 --> 00:06:59,030
But by the time we reach this input,

101
00:06:59,120 --> 00:07:03,080
it would be really good if this actually read one instead of zero.

102
00:07:03,620 --> 00:07:07,160
So one way we could do this is we could do this plus one,

103
00:07:07,610 --> 00:07:12,140
but remembering that we actually need to increase the question number every

104
00:07:12,140 --> 00:07:14,570
single time we call next_question anyways,

105
00:07:14,930 --> 00:07:17,870
then once we've gotten hold of the current question,

106
00:07:18,170 --> 00:07:23,170
we can actually tap into that self.question_number and increase it by one.

107
00:07:23,900 --> 00:07:25,610
This will have the same effect,

108
00:07:25,760 --> 00:07:30,320
and if you look at it now we've got the right question number being shown here,

109
00:07:30,680 --> 00:07:35,570
the right text being shown, and then we can insert true or false.

110
00:07:36,680 --> 00:07:37,760
Now at this stage,

111
00:07:37,790 --> 00:07:42,020
our program is just going to exit. In the next lesson,

112
00:07:42,260 --> 00:07:47,260
we're going to have to work out a way for our questions to keep being asked. And

113
00:07:48,080 --> 00:07:50,840
we'll need to write some more code for that to work.

