So I'm sure you've had a few gos with your quiz now
and you've answered all the questions and you know all the correct answers.
It's a little bit dry if we only have one question bank, right?
Wouldn't it be nice to be able to switch out the question data with some new
questions? Now you could certainly write your own questions,
just change each of the texts and the answers to whatever question you want to
add. An alternative though, is to use the Open Trivia database,
which is a free-to-use user contributed trivia question database
and it has over 3000 verified questions to pick from. In the course resources
I've got a link to the open TDB, the open trivia database,
and we can go ahead and take a look at their API to see how we can generate some
questions. For example, if I wanted 10 questions on,
let's say computers and I want the difficulty to be easy
and I want to select the type to be true or false
And then I'm going to go and click generate API URL.
Now that I've got my URL,
I can open up a new tab and go to that location.
Now, we'll get some data generated in a JSON format
which is a JavaScript object notation
but it actually looks pretty much the same as a Python dictionary.
So let's go ahead and copy everything that's here and then go into our data.py
and I'm going to replace the entire question data with what I got just now.
Now once you paste it in, it's going to be very hard to read.
So we'll need to go to code and then reformat code in order to get it to look a
little bit more normal.
So you can see that we've got this dictionary and it has two key-value pairs.
One is a response code and another is a list which is under the key results.
If I go and reformat this again,
you can see that results is comprised of 10 dictionaries.
Now,
each of these dictionaries contain a whole bunch of key-value pairs and inside
each of these key-value pairs it has a bunch of things
including the category, that type, the difficulty,
the question, the correct answer, and the incorrect answers.
So what we can see here is we have a dictionary and then we have these two key
value pairs.
So you can simply just get rid of the enclosing dictionary and leave it so that
you have this as a list of dictionaries. Now,
if we go and reformat our code so that we can see each of these dictionary
objects,
you can see there's a total of five key value pairs and they each have the keys
category, type, question, which is the question text and the correct answer
which is a string that's true or false.
These are the two bits of information that we're interested in.
So can you figure out how you can modify the main.py in order to get our
quiz to start working again with this new data?
Pause the video and complete this challenge.
All right. So we've got our question data from the data file,
but now we have to change the names of these keys.
Whereas before it was called text and answer,
now it's called question and correct answer.
So the question_text is under the key question and correct answer is under
the key correct _answer.
So just by changing those two things and pasting all of that data in
we can already get started with this brand new quiz. And you can play with it and
mess around with different data from the open trivia database,
change the category, change the difficulty,
and you have endless number of questions to play with.
And this really brings about some of the advantages of Object Oriented
Programming. Notice how only our main.py file actually has knowledge of how each
of these classes work and behave. Our QuizBrain actually didn't need to be
touched at all
when we changed our data over. This is modularity at its best.
We're able to completely switch up the question data to a different language,
to a different topic, to a different format, and the quiz brain doesn't care.
All it has to concern itself with is how to track which question we're on,
how to get the next question, how to check the answer.
And as long as it's able to do that and perform the functionality of a quiz,
it's unconcerned by where the data comes from, how it's formatted,
and it will continue to work. So, as you can imagine,
if your colleague was working on quiz brain,
they don't actually need to know how the data looks or how it's structured.
And you, on the other hand, could be working on the data,
getting hold of each of these pieces of data or writing the quiz.
And you can already see how we've got a piece of program
that's able to be far more complex than what we used to be able to create with
simple procedural code.