Now that we've managed to create a question bank
which consists of a list of question
objects, the next natural thing to do is to actually bring up one of those
questions and to ask the user to answer the question. Now,
for all of the questioning and quizzing functionality,
we're going to put that into the QuizBrain,
which is going to be a class in its own right.
And it's going to manage everything from asking the user for the next question,
checking whether if the answer was right and also checking to see if we're
actually at the end of the quiz.
You're going to be creating this QuizBrain and class
and this class is going to have two attributes to begin with; a question number
which is going to have a default value of zero
because all our quizzes will start from the first question.
And this is going to keep track of which question the user is currently on.
And we're going to use that number to go through the list of questions
which will be passed over to this QuizBrain object when it gets initialized.
And then you're going to have a method which is called next_question
which will pull up the question from that list
depending on which current question number we're on.
If you take a look at the final version of the quiz game,
you can see that it displays the question number, Q. and then that number.
And then it displays the question text,
and then it asks the user for an input, true or false.
This is what we're going to be focusing on in this lesson.
Inside your quiz-game-start,
go to the quiz_brain file and create the quiz brain class.
Add those two attributes; one is the question number
which starts out with a default value and the other is the question list
which is going to be initialized when you create a new quiz brain.
And we're going to be passing over this question bank here into this question
list when we initialize a new QuizBrain.
Pause the video and see if you can complete this challenge.
Right?
All right. So let's start off by creating our new class
which is going to be called QuizBrain and inside this class, we'll need to
initialize some of those attributes,
so I'm going to create the init method. Inside the init method
I'm going to create that first attribute which was called question_number
and I said we would set it to have a default value of zero.
So this means every time we create a new QuizBrain
object from this class,
it's ready going to have an attribute called question_number that set to zero.
Now the next attribute is called question_list,
and this is not going to have a default value. Instead,
it's going to get it as a value passed over.
So lets put that as a q_list here and set it equal to question_list.
So what's going to happen is inside our main.py file,
we're going to be initializing that QuizBrain and the value that we're going to
put in there as the question list is going to be the question bank. This way
we'll receive the question bank and it will be inside this question list
attribute. Now I have another challenge for you.
Create a method called next_question inside the QuizBrain.
This method needs to retrieve the item at the current question number from the
question list. And once you have this item,
use the input function to show the user the question text and ask for the
user's answer. Pause the video and give this a go.
Alright, here's the solution.
First we're gonna define a new method called the next_question,
and this method is basically going to get hold of the current question and that
is of course going to be from the question list.
And then we're going to tap into that list and get hold of the item at the
current question_number. So for example,
question_number is going to start out being zero
and so it's going to go into this list
which is basically going to be our list of questions from our main.py,
so our question bank.
And remember, each of the items inside that list is a question object and
question objects have text attributes and answer attributes.
So in addition to getting hold of the current question,
we can then get hold of the current question text by taking the current question
and then just simply writing .text to get the value that's saved in that
attribute.
We're going to use an input to ask the user for
what their answer is to this question.
This is going to be an fstring because we're going to be inserting the current_
question.text. And at the very beginning
if you remember,
we also have the Q. and then the number and then the colon.
So that will be the self.question_number.
And then we've got our colon and then it's going to be the question text.
And then we're going to ask the user for a input; true or false.
And that's it. That completes the challenge.
Now all that's left is to show the very first question to the user and run our
code. Let's go back to the main.py and import the QuizBrain class.
So we're going to say from the quiz_brain file import the QuizBrain class.
And once we've created our question bank,
then the next thing to do is to create our new quiz.
So this is going to be a new QuizBrain object
and when we initialize it, we have to pass in a list of questions.
And that is of course going to be the question bank.
So now that we've got our quiz up and running,
then we can say quiz.next_question.
Let's go ahead and run this main.py file.
And you can see we get our question text being displayed, true or false being
asked, but our question number starts from zero.
And that is, of course,
because we need zero here in order to get hold of the first question from the
list and that's why this has a default value of zero.
But by the time we reach this input,
it would be really good if this actually read one instead of zero.
So one way we could do this is we could do this plus one,
but remembering that we actually need to increase the question number every
single time we call next_question anyways,
then once we've gotten hold of the current question,
we can actually tap into that self.question_number and increase it by one.
This will have the same effect,
and if you look at it now we've got the right question number being shown here,
the right text being shown, and then we can insert true or false.
Now at this stage,
our program is just going to exit. In the next lesson,
we're going to have to work out a way for our questions to keep being asked. And
we'll need to write some more code for that to work.