1
00:00:00,330 --> 00:00:01,650
Now in the last lesson,

2
00:00:01,680 --> 00:00:05,610
we created our question class. In this lesson,

3
00:00:05,640 --> 00:00:10,470
we're going to be creating a question bank of question objects.

4
00:00:11,070 --> 00:00:15,930
Notice how we can create a question object by using the name of the class and

5
00:00:15,930 --> 00:00:20,250
then giving it some inputs for the question and the answer. Now,

6
00:00:20,250 --> 00:00:25,080
if we create a whole bunch of those and then put them into a list like this,

7
00:00:25,140 --> 00:00:29,580
then we can create a question bank of questions to use in our quiz.

8
00:00:30,720 --> 00:00:33,570
If you take a look inside the data.py file,

9
00:00:33,900 --> 00:00:38,900
you'll see that I've already included a whole bunch of questions inside here,

10
00:00:39,270 --> 00:00:42,300
and you can see it's got some text and it's got some answer.

11
00:00:42,780 --> 00:00:47,780
So the exact structure of this file is that it's got a whole bunch of

12
00:00:48,360 --> 00:00:51,030
dictionaries, 12 in total,

13
00:00:51,330 --> 00:00:55,110
and all of these dictionaries are saved inside a list

14
00:00:55,440 --> 00:01:00,270
and that list is called question_data. Now, when you first come onto this file

15
00:01:00,300 --> 00:01:04,019
you'll see some warnings, these little yellow squiggly lines.

16
00:01:04,500 --> 00:01:08,190
And the reason is because it would expect for proper formatting,

17
00:01:08,580 --> 00:01:13,140
each of these dictionaries should be indented inside the list. Now,

18
00:01:13,200 --> 00:01:16,620
in terms of actual functionality, this won't change anything.

19
00:01:16,650 --> 00:01:21,650
The indentation of the items in a list are not as important as the indentation

20
00:01:21,810 --> 00:01:23,820
in say a method or a for loop.

21
00:01:24,360 --> 00:01:27,210
But it's still better to get rid of all of these warnings.

22
00:01:27,630 --> 00:01:32,250
So what we're going to do is we're going to select everything inside this file,

23
00:01:32,310 --> 00:01:35,520
we're going to go to code and then auto-indent lines.

24
00:01:36,090 --> 00:01:39,060
And this you'll notice has now auto-indented

25
00:01:39,330 --> 00:01:42,120
all of the dictionaries inside the list.

26
00:01:42,720 --> 00:01:47,190
Now you'll still notice some red squiggly lines. So in this case,

27
00:01:47,190 --> 00:01:51,690
the reason is because the line is too long, it's greater than 120 characters.

28
00:01:51,960 --> 00:01:55,260
You can see that's where the cutoff is. So in PyCharm

29
00:01:55,260 --> 00:01:57,390
it's really easy to separate lines.

30
00:01:57,420 --> 00:02:01,020
If you just identify a good spot in that line, hit enter,

31
00:02:01,050 --> 00:02:05,700
and it will automatically add some quotation marks around it so that it still

32
00:02:05,700 --> 00:02:09,660
behaves the same way as before, but now it's on separate lines.

33
00:02:10,090 --> 00:02:12,450
And I'm going to do the same to line 8 as well.

34
00:02:12,840 --> 00:02:16,440
Now the final warning expect a new line at the end of the file,

35
00:02:16,650 --> 00:02:17,790
so it let's give it that.

36
00:02:18,150 --> 00:02:22,710
And the last thing that you'll notice here is that there's a squiggly line here

37
00:02:22,740 --> 00:02:26,880
under Backrub because that is a unknown word in the dictionary.

38
00:02:27,240 --> 00:02:31,050
So if you want to, you can actually save words into the dictionary

39
00:02:31,320 --> 00:02:33,420
to you get rid of all of these squiggly lines.

40
00:02:33,780 --> 00:02:35,940
This is actually a name of a company,

41
00:02:35,940 --> 00:02:38,280
so it's probably not going to be in the dictionary.

42
00:02:38,580 --> 00:02:41,250
But if you go ahead and put your cursor on it like this,

43
00:02:41,490 --> 00:02:45,720
you'll see a little light-bulb light up and you can go ahead and select save 

44
00:02:45,720 --> 00:02:47,490
'Backrub' to project-level dictionary,

45
00:02:47,910 --> 00:02:50,850
and that will get rid of all of your warnings and errors

46
00:02:51,120 --> 00:02:54,000
and you'll see a nice green check mark in the right corner.

47
00:02:55,110 --> 00:02:57,990
Now that we've done all of this formatting with the data,

48
00:02:58,020 --> 00:03:03,020
let's really examine it. What we have here is a list of dictionaries.

49
00:03:04,390 --> 00:03:09,390
Each of the dictionaries have the same key for the question text and the same

50
00:03:09,640 --> 00:03:11,320
key for the question answer.

51
00:03:11,890 --> 00:03:16,890
So what we need to do is we need to be able to bring this question data into the

52
00:03:17,560 --> 00:03:21,850
main.py file. But instead of having a list of dictionaries,

53
00:03:22,240 --> 00:03:25,120
we want a list of question objects.

54
00:03:25,690 --> 00:03:30,690
We know already that we can create a new question object by simply constructing

55
00:03:31,690 --> 00:03:35,740
one from the question and then giving it the required inputs.

56
00:03:36,160 --> 00:03:39,700
But in order to do this inside the main.py file,

57
00:03:40,420 --> 00:03:45,420
we, of course, have to import the question model and we'll also need to import the

58
00:03:45,790 --> 00:03:49,480
data.py. So from the question model

59
00:03:49,480 --> 00:03:54,310
I'm going to import the question class, and from the data file

60
00:03:54,340 --> 00:03:56,830
I'm going to import the question data.

61
00:03:57,550 --> 00:04:02,260
Now that you've got both the question data and the question class,

62
00:04:02,950 --> 00:04:06,010
your goal is to create the question bank.

63
00:04:06,670 --> 00:04:08,020
And once you're done,

64
00:04:08,200 --> 00:04:12,100
it should contain a list of question objects

65
00:04:12,520 --> 00:04:15,610
each being initialized with a question and an answer,

66
00:04:16,390 --> 00:04:21,390
and the data is going to come from these dictionaries from the question data.

67
00:04:22,450 --> 00:04:26,440
So you'll need to think about how you can loop through that question data in

68
00:04:26,440 --> 00:04:28,930
order to create this list of question objects.

69
00:04:29,440 --> 00:04:31,510
Pause the video and give that a go now.

70
00:04:34,930 --> 00:04:37,690
All right. So we know that we need to create this question bank

71
00:04:37,750 --> 00:04:40,600
which is going to be a list of question objects.

72
00:04:41,110 --> 00:04:43,930
So I'm going to start out just by creating an empty list

73
00:04:44,380 --> 00:04:49,380
and then I'm going to loop through each of the questions inside the question

74
00:04:49,720 --> 00:04:53,380
data. And for each of these questions,

75
00:04:53,380 --> 00:04:58,180
I'm going to create a variable, which we'll call a question_text

76
00:04:58,870 --> 00:05:03,870
and the question_text is going to come from those question dictionaries from

77
00:05:04,660 --> 00:05:06,730
here. And if we imagine

78
00:05:06,730 --> 00:05:10,330
we have one of these dictionaries and we want to get hold of the question_text,

79
00:05:10,660 --> 00:05:12,910
then we'll need to tap into the text key.

80
00:05:13,390 --> 00:05:18,130
So I'm going to put text inside here and I'm going to do the same thing for the

81
00:05:18,130 --> 00:05:18,963
answer.

82
00:05:19,570 --> 00:05:24,570
So this is going to be using the answer key that we see right here.

83
00:05:25,600 --> 00:05:29,500
So now if we're looping through the first one, we should have 'A slug's

84
00:05:29,500 --> 00:05:34,500
blood is green.' and 'True' for these two variables.

85
00:05:35,080 --> 00:05:38,230
So now I'm ready to create my new question,

86
00:05:39,040 --> 00:05:41,980
which is going to be created from the question class.

87
00:05:42,430 --> 00:05:46,660
And you can see that in the constructor, it's expecting two parameters;

88
00:05:46,930 --> 00:05:51,930
one is the q_text and another is the q_answer.

89
00:05:53,650 --> 00:05:58,340
We can set the q_text to this local variable called question_text,

90
00:05:58,760 --> 00:06:02,750
and we can set the answer to the question_answer. And of course,

91
00:06:02,750 --> 00:06:04,100
for simplicity sake,

92
00:06:04,130 --> 00:06:09,130
we can also get rid of the named parameters and have them just as the parameters

93
00:06:09,400 --> 00:06:10,233
like this.

94
00:06:10,630 --> 00:06:15,630
So now that we've created this new question object and saved it in the variable

95
00:06:16,000 --> 00:06:19,690
new_question, we're ready to add it into our question bank.

96
00:06:19,870 --> 00:06:22,060
So we can say a question_bank.append,

97
00:06:22,450 --> 00:06:26,560
and we'll append each of these new questions that we create in the for loop.

98
00:06:27,190 --> 00:06:30,640
So now if I go ahead and print this question bank,

99
00:06:31,120 --> 00:06:35,350
you'll see that when I hit run and select my main file,

100
00:06:35,830 --> 00:06:40,830
then it prints out a list denoted by the square brackets and inside the list

101
00:06:41,830 --> 00:06:46,090
you've got a whole bunch of question objects. And if you count,

102
00:06:46,120 --> 00:06:47,920
there's actually 12 in total,

103
00:06:48,190 --> 00:06:52,060
one for each of the dictionaries inside our question_data.

104
00:06:52,750 --> 00:06:55,600
So very often when you're getting data from the

105
00:06:55,810 --> 00:06:59,410
internet or from somewhere else, it'll likely be in a format

106
00:06:59,410 --> 00:07:04,210
that's very similar to this. What we're doing is we're converting each of these

107
00:07:04,210 --> 00:07:08,500
pieces of data which has a string key,

108
00:07:08,680 --> 00:07:10,630
which means it's very easy to make typos

109
00:07:10,630 --> 00:07:12,880
if we're using this in our code everywhere,

110
00:07:13,270 --> 00:07:15,760
and we're converting it into a object

111
00:07:16,120 --> 00:07:21,120
which now has all of that data in a very easy and foolproof way of accessing the

112
00:07:22,660 --> 00:07:26,380
text and the answer. And you'll notice here,

113
00:07:26,410 --> 00:07:29,050
if I actually write the wrong thing,

114
00:07:29,320 --> 00:07:31,810
then it's actually going to highlight to me

115
00:07:32,080 --> 00:07:35,980
this is not something that you can use. And if I start typing,

116
00:07:36,010 --> 00:07:38,920
I've also got the help of the auto-insert.

117
00:07:40,480 --> 00:07:45,480
Now that we've created our question bank of question objects, the next step is to

118
00:07:46,240 --> 00:07:48,160
actually get our quiz up and running.

