Now in the last lesson,
we created our question class. In this lesson,
we're going to be creating a question bank of question objects.
Notice how we can create a question object by using the name of the class and
then giving it some inputs for the question and the answer. Now,
if we create a whole bunch of those and then put them into a list like this,
then we can create a question bank of questions to use in our quiz.
If you take a look inside the data.py file,
you'll see that I've already included a whole bunch of questions inside here,
and you can see it's got some text and it's got some answer.
So the exact structure of this file is that it's got a whole bunch of
dictionaries, 12 in total,
and all of these dictionaries are saved inside a list
and that list is called question_data. Now, when you first come onto this file
you'll see some warnings, these little yellow squiggly lines.
And the reason is because it would expect for proper formatting,
each of these dictionaries should be indented inside the list. Now,
in terms of actual functionality, this won't change anything.
The indentation of the items in a list are not as important as the indentation
in say a method or a for loop.
But it's still better to get rid of all of these warnings.
So what we're going to do is we're going to select everything inside this file,
we're going to go to code and then auto-indent lines.
And this you'll notice has now auto-indented
all of the dictionaries inside the list.
Now you'll still notice some red squiggly lines. So in this case,
the reason is because the line is too long, it's greater than 120 characters.
You can see that's where the cutoff is. So in PyCharm
it's really easy to separate lines.
If you just identify a good spot in that line, hit enter,
and it will automatically add some quotation marks around it so that it still
behaves the same way as before, but now it's on separate lines.
And I'm going to do the same to line 8 as well.
Now the final warning expect a new line at the end of the file,
so it let's give it that.
And the last thing that you'll notice here is that there's a squiggly line here
under Backrub because that is a unknown word in the dictionary.
So if you want to, you can actually save words into the dictionary
to you get rid of all of these squiggly lines.
This is actually a name of a company,
so it's probably not going to be in the dictionary.
But if you go ahead and put your cursor on it like this,
you'll see a little light-bulb light up and you can go ahead and select save
'Backrub' to project-level dictionary,
and that will get rid of all of your warnings and errors
and you'll see a nice green check mark in the right corner.
Now that we've done all of this formatting with the data,
let's really examine it. What we have here is a list of dictionaries.
Each of the dictionaries have the same key for the question text and the same
key for the question answer.
So what we need to do is we need to be able to bring this question data into the
main.py file. But instead of having a list of dictionaries,
we want a list of question objects.
We know already that we can create a new question object by simply constructing
one from the question and then giving it the required inputs.
But in order to do this inside the main.py file,
we, of course, have to import the question model and we'll also need to import the
data.py. So from the question model
I'm going to import the question class, and from the data file
I'm going to import the question data.
Now that you've got both the question data and the question class,
your goal is to create the question bank.
And once you're done,
it should contain a list of question objects
each being initialized with a question and an answer,
and the data is going to come from these dictionaries from the question data.
So you'll need to think about how you can loop through that question data in
order to create this list of question objects.
Pause the video and give that a go now.
All right. So we know that we need to create this question bank
which is going to be a list of question objects.
So I'm going to start out just by creating an empty list
and then I'm going to loop through each of the questions inside the question
data. And for each of these questions,
I'm going to create a variable, which we'll call a question_text
and the question_text is going to come from those question dictionaries from
here. And if we imagine
we have one of these dictionaries and we want to get hold of the question_text,
then we'll need to tap into the text key.
So I'm going to put text inside here and I'm going to do the same thing for the
answer.
So this is going to be using the answer key that we see right here.
So now if we're looping through the first one, we should have 'A slug's
blood is green.' and 'True' for these two variables.
So now I'm ready to create my new question,
which is going to be created from the question class.
And you can see that in the constructor, it's expecting two parameters;
one is the q_text and another is the q_answer.
We can set the q_text to this local variable called question_text,
and we can set the answer to the question_answer. And of course,
for simplicity sake,
we can also get rid of the named parameters and have them just as the parameters
like this.
So now that we've created this new question object and saved it in the variable
new_question, we're ready to add it into our question bank.
So we can say a question_bank.append,
and we'll append each of these new questions that we create in the for loop.
So now if I go ahead and print this question bank,
you'll see that when I hit run and select my main file,
then it prints out a list denoted by the square brackets and inside the list
you've got a whole bunch of question objects. And if you count,
there's actually 12 in total,
one for each of the dictionaries inside our question_data.
So very often when you're getting data from the
internet or from somewhere else, it'll likely be in a format
that's very similar to this. What we're doing is we're converting each of these
pieces of data which has a string key,
which means it's very easy to make typos
if we're using this in our code everywhere,
and we're converting it into a object
which now has all of that data in a very easy and foolproof way of accessing the
text and the answer. And you'll notice here,
if I actually write the wrong thing,
then it's actually going to highlight to me
this is not something that you can use. And if I start typing,
I've also got the help of the auto-insert.
Now that we've created our question bank of question objects, the next step is to
actually get our quiz up and running.