Now that we've seen how to create our own custom classes,
we're finally ready to build the quiz game.
Now this game is going to be a simple true/false game.
So a question will be asked to the user and they'll select true or false,
and then we'll tell them if they got it right or wrong and give them a score.
Now to begin, the first step you'll need to do is to download the zip
file it with all the starting code from our course resource website.
Alternatively,
if you are already logged in on Repl.it simply fork the project and then click
on these three dots to download the zip file here.
And once you've extracted the actual folder, quiz-game-
start from the.zip file, then we're ready to go ahead and open it
using PyCharm. In PyCharm
we're going to click open and then we're going to navigate to the place where we
have our quiz-game-start, which in my case is in the downloads folder.
PyCharm should automatically configure the latest version of Python you've got
installed as the interpreter.
So you can go ahead and close these popups.
And if we open up our quiz-game-start folder,
you can see we have four files, data,
main, question_model and quiz_brain.
And over the course of the next few lessons,
we're going to be writing the code in all of these files so that we end up
creating our final quiz project.
The first task that we have is to create a model for a question in our quiz.
If we had a question object, well what kind of attributes should it have?
It might have a text attribute for the question texts,
and it might also hold an answer attribute
which is going to be the correct answer to that question. Now,
these two attributes should be initialized with a value
when a new question object is created from this class.
For example, if our first question was 2 + 3 = 5,
which we know the correct answer should be true. Well,
then we might write the code like this to actually initialize a new question
object. And when this initialization goes through the constructor code inside the
question class
will take these two pieces of data and then add them to the corresponding
attributes, the text and the answer.
And so our new question object will have both of these attributes initialized
with a value
when a new question object is being created. Inside your project,
go over to the question_model.py file,
and go ahead and create a new class called question
and that question class should have an init method
which will initialize two attributes,
the text and the answer just as you've seen in the previous slides.
Pause the video and see if you can complete this challenge by creating this
class.
Right.
All right. So we know that to create a class,
we first have to use the class keyword. And then in our case,
the name of the class is going to be called Question because we're modeling what
a question would be like in our game.
We know that each of these questions have two attributes;
a self.text and a self.answer.
And those two attributes are going to be initialized
when we create a new question object. So we need the init function in here.
And we're pretty much going to use the autofill.
So once you write Def and then you write init,
you can see it in the dropdown list and then just hit enter
and the code will be inserted automatically. This way
you don't have to remember the exact order of the parentheses or where to put
the colon, et cetera, et cetera. Now, inside the init function,
we're going to set up two attributes as we said.
One is going to be called text and one is going to be called answer.
And if we want to create an attribute, we have to use this syntax,
self. and then the name of the attribute,
because eventually when we create a new object from this class
say a new question object, then we're going to be passing in these two items,
a piece of text which is going to be the question,
and then either true or false as the answer. And then later on
if we wanted to access the text,
then we would say new_q.text.
Now of course at the moment,
these are all yellowed out because it seems to have no effect.
There's no way of receiving a value for each of these attributes.
And to do that, we have to add some inputs inside the init function.
So let's just call it q_text and a q_answer.
You can of course call these parameters anything you want.
But the important part is you set the self.text to that first value and the
self.answer to the second value.
So now when we actually go ahead and print this new_q.text
object like we have here,
then it will go ahead and actually fetch this value into the output.
If you go to run and then run and then select a question model,
you can see that is what gets printed in the console.
Let's delete those two lines and now we have completed our first task,
which is creating our question class from scratch.