WEBVTT

0
00:00.330 --> 00:01.650
Now in the last lesson,

1
00:01.680 --> 00:05.610
we created our question class. In this lesson,

2
00:05.640 --> 00:10.470
we're going to be creating a question bank of question objects.

3
00:11.070 --> 00:15.930
Notice how we can create a question object by using the name of the class and

4
00:15.930 --> 00:20.250
then giving it some inputs for the question and the answer. Now,

5
00:20.250 --> 00:25.080
if we create a whole bunch of those and then put them into a list like this,

6
00:25.140 --> 00:29.580
then we can create a question bank of questions to use in our quiz.

7
00:30.720 --> 00:33.570
If you take a look inside the data.py file,

8
00:33.900 --> 00:38.900
you'll see that I've already included a whole bunch of questions inside here,

9
00:39.270 --> 00:42.300
and you can see it's got some text and it's got some answer.

10
00:42.780 --> 00:47.780
So the exact structure of this file is that it's got a whole bunch of

11
00:48.360 --> 00:51.030
dictionaries, 12 in total,

12
00:51.330 --> 00:55.110
and all of these dictionaries are saved inside a list

13
00:55.440 --> 01:00.270
and that list is called question_data. Now, when you first come onto this file

14
01:00.300 --> 01:04.020
you'll see some warnings, these little yellow squiggly lines.

15
01:04.500 --> 01:08.190
And the reason is because it would expect for proper formatting,

16
01:08.580 --> 01:13.140
each of these dictionaries should be indented inside the list. Now,

17
01:13.200 --> 01:16.620
in terms of actual functionality, this won't change anything.

18
01:16.650 --> 01:21.650
The indentation of the items in a list are not as important as the indentation

19
01:21.810 --> 01:23.820
in say a method or a for loop.

20
01:24.360 --> 01:27.210
But it's still better to get rid of all of these warnings.

21
01:27.630 --> 01:32.250
So what we're going to do is we're going to select everything inside this file,

22
01:32.310 --> 01:35.520
we're going to go to code and then auto-indent lines.

23
01:36.090 --> 01:39.060
And this you'll notice has now auto-indented

24
01:39.330 --> 01:42.120
all of the dictionaries inside the list.

25
01:42.720 --> 01:47.190
Now you'll still notice some red squiggly lines. So in this case,

26
01:47.190 --> 01:51.690
the reason is because the line is too long, it's greater than 120 characters.

27
01:51.960 --> 01:55.260
You can see that's where the cutoff is. So in PyCharm

28
01:55.260 --> 01:57.390
it's really easy to separate lines.

29
01:57.420 --> 02:01.020
If you just identify a good spot in that line, hit enter,

30
02:01.050 --> 02:05.700
and it will automatically add some quotation marks around it so that it still

31
02:05.700 --> 02:09.660
behaves the same way as before, but now it's on separate lines.

32
02:10.090 --> 02:12.450
And I'm going to do the same to line 8 as well.

33
02:12.840 --> 02:16.440
Now the final warning expect a new line at the end of the file,

34
02:16.650 --> 02:17.790
so it let's give it that.

35
02:18.150 --> 02:22.710
And the last thing that you'll notice here is that there's a squiggly line here

36
02:22.740 --> 02:26.880
under Backrub because that is a unknown word in the dictionary.

37
02:27.240 --> 02:31.050
So if you want to, you can actually save words into the dictionary

38
02:31.320 --> 02:33.420
to you get rid of all of these squiggly lines.

39
02:33.780 --> 02:35.940
This is actually a name of a company,

40
02:35.940 --> 02:38.280
so it's probably not going to be in the dictionary.

41
02:38.580 --> 02:41.250
But if you go ahead and put your cursor on it like this,

42
02:41.490 --> 02:45.720
you'll see a little light-bulb light up and you can go ahead and select save 

43
02:45.720 --> 02:47.490
'Backrub' to project-level dictionary,

44
02:47.910 --> 02:50.850
and that will get rid of all of your warnings and errors

45
02:51.120 --> 02:54.000
and you'll see a nice green check mark in the right corner.

46
02:55.110 --> 02:57.990
Now that we've done all of this formatting with the data,

47
02:58.020 --> 03:03.020
let's really examine it. What we have here is a list of dictionaries.

48
03:04.390 --> 03:09.390
Each of the dictionaries have the same key for the question text and the same

49
03:09.640 --> 03:11.320
key for the question answer.

50
03:11.890 --> 03:16.890
So what we need to do is we need to be able to bring this question data into the

51
03:17.560 --> 03:21.850
main.py file. But instead of having a list of dictionaries,

52
03:22.240 --> 03:25.120
we want a list of question objects.

53
03:25.690 --> 03:30.690
We know already that we can create a new question object by simply constructing

54
03:31.690 --> 03:35.740
one from the question and then giving it the required inputs.

55
03:36.160 --> 03:39.700
But in order to do this inside the main.py file,

56
03:40.420 --> 03:45.420
we, of course, have to import the question model and we'll also need to import the

57
03:45.790 --> 03:49.480
data.py. So from the question model

58
03:49.480 --> 03:54.310
I'm going to import the question class, and from the data file

59
03:54.340 --> 03:56.830
I'm going to import the question data.

60
03:57.550 --> 04:02.260
Now that you've got both the question data and the question class,

61
04:02.950 --> 04:06.010
your goal is to create the question bank.

62
04:06.670 --> 04:08.020
And once you're done,

63
04:08.200 --> 04:12.100
it should contain a list of question objects

64
04:12.520 --> 04:15.610
each being initialized with a question and an answer,

65
04:16.390 --> 04:21.390
and the data is going to come from these dictionaries from the question data.

66
04:22.450 --> 04:26.440
So you'll need to think about how you can loop through that question data in

67
04:26.440 --> 04:28.930
order to create this list of question objects.

68
04:29.440 --> 04:31.510
Pause the video and give that a go now.

69
04:34.930 --> 04:37.690
All right. So we know that we need to create this question bank

70
04:37.750 --> 04:40.600
which is going to be a list of question objects.

71
04:41.110 --> 04:43.930
So I'm going to start out just by creating an empty list

72
04:44.380 --> 04:49.380
and then I'm going to loop through each of the questions inside the question

73
04:49.720 --> 04:53.380
data. And for each of these questions,

74
04:53.380 --> 04:58.180
I'm going to create a variable, which we'll call a question_text

75
04:58.870 --> 05:03.870
and the question_text is going to come from those question dictionaries from

76
05:04.660 --> 05:06.730
here. And if we imagine

77
05:06.730 --> 05:10.330
we have one of these dictionaries and we want to get hold of the question_text,

78
05:10.660 --> 05:12.910
then we'll need to tap into the text key.

79
05:13.390 --> 05:18.130
So I'm going to put text inside here and I'm going to do the same thing for the

80
05:18.130 --> 05:18.963
answer.

81
05:19.570 --> 05:24.570
So this is going to be using the answer key that we see right here.

82
05:25.600 --> 05:29.500
So now if we're looping through the first one, we should have 'A slug's

83
05:29.500 --> 05:34.500
blood is green.' and 'True' for these two variables.

84
05:35.080 --> 05:38.230
So now I'm ready to create my new question,

85
05:39.040 --> 05:41.980
which is going to be created from the question class.

86
05:42.430 --> 05:46.660
And you can see that in the constructor, it's expecting two parameters;

87
05:46.930 --> 05:51.930
one is the q_text and another is the q_answer.

88
05:53.650 --> 05:58.340
We can set the q_text to this local variable called question_text,

89
05:58.760 --> 06:02.750
and we can set the answer to the question_answer. And of course,

90
06:02.750 --> 06:04.100
for simplicity sake,

91
06:04.130 --> 06:09.130
we can also get rid of the named parameters and have them just as the parameters

92
06:09.400 --> 06:10.233
like this.

93
06:10.630 --> 06:15.630
So now that we've created this new question object and saved it in the variable

94
06:16.000 --> 06:19.690
new_question, we're ready to add it into our question bank.

95
06:19.870 --> 06:22.060
So we can say a question_bank.append,

96
06:22.450 --> 06:26.560
and we'll append each of these new questions that we create in the for loop.

97
06:27.190 --> 06:30.640
So now if I go ahead and print this question bank,

98
06:31.120 --> 06:35.350
you'll see that when I hit run and select my main file,

99
06:35.830 --> 06:40.830
then it prints out a list denoted by the square brackets and inside the list

100
06:41.830 --> 06:46.090
you've got a whole bunch of question objects. And if you count,

101
06:46.120 --> 06:47.920
there's actually 12 in total,

102
06:48.190 --> 06:52.060
one for each of the dictionaries inside our question_data.

103
06:52.750 --> 06:55.600
So very often when you're getting data from the

104
06:55.810 --> 06:59.410
internet or from somewhere else, it'll likely be in a format

105
06:59.410 --> 07:04.210
that's very similar to this. What we're doing is we're converting each of these

106
07:04.210 --> 07:08.500
pieces of data which has a string key,

107
07:08.680 --> 07:10.630
which means it's very easy to make typos

108
07:10.630 --> 07:12.880
if we're using this in our code everywhere,

109
07:13.270 --> 07:15.760
and we're converting it into a object

110
07:16.120 --> 07:21.120
which now has all of that data in a very easy and foolproof way of accessing the

111
07:22.660 --> 07:26.380
text and the answer. And you'll notice here,

112
07:26.410 --> 07:29.050
if I actually write the wrong thing,

113
07:29.320 --> 07:31.810
then it's actually going to highlight to me

114
07:32.080 --> 07:35.980
this is not something that you can use. And if I start typing,

115
07:36.010 --> 07:38.920
I've also got the help of the auto-insert.

116
07:40.480 --> 07:45.480
Now that we've created our question bank of question objects, the next step is to

117
07:46.240 --> 07:48.160
actually get our quiz up and running.