To begin, head over to the course resources as always and download the starting
zip file for today's starting project. And once you've done that,
go ahead and unzip the file and open it up inside PyCharm. Now,
if we take a look inside the project folder,
you can see we've got a bunch of images
which is going to be used for the buttons,
and we've also got a lot of the code that we had created in our previous quiz
project. It may seem like it was a long time ago,
but back in day 17
we learned about creating classes and the project that we built to do that was a
text based quiz. And in fact,
once you've downloaded and opened up the starting file for today's project,
you can actually run this program.
So open up main.py and run it.
Then you can see the original format of the quiz application in action
inside the console. We can type true or false,
and it will give us some feedback, whether if we got it right
or whether if we got it wrong and also our current score.
Now this was great on day 17,
but now that we know all about creating graphical user interfaces using tkinter
as well as a whole bunch of things that we've learned about APIs just now, we're
going to upgrade this quiz app to the next level.
The first thing we want to upgrade is at the moment,
these questions are hard-coded.
So inside our data.py,
you can see all of these questions as a list of dictionary objects.
And at the time,
the place where we got all of these questions from was in fact,
the open trivia database.
But what we did here is we selected the number of questions we wanted,
we selected the category that we wanted,
and then we changed the type to true and false and we said, generate API URL.
And then once that URL is generated,
all we did was we put it into our browser and we got this data and we put it
into our question data. Now,
this, being hard-coded, is obviously never going to change.
So what we want to do instead is we want to get 10 questions from the open
trivia database,
but this time we want to make a request to the API so that we always get
different questions and different answers.
The idea of this challenge is to modify the code in the data.py file to get 10
new questions using the open trivia database every time the program is run.
Here's what your code should do after you've successfully completed the
challenge. You'll still get a console based quiz, but this time,
the text and the correct answer came from the open trivia database via an API
call.
You'll see that any symbol like single double quotes will not be displayed
correctly just yet,
but getting the API call working is the first step and the goal of this
challenge.
So what I want you to do is to set the number of questions to 10 and leave the
category as blank, leave the difficulty is blank,
but make sure that the type is set to true-false and then generate this API URL.
Now remember what I said before that the API end point is everything before the
question mark. So this is the base URL that we're going to be working with.
And then after the question mark, we've got our different parameters.
So we've got a parameter called amount
which is set to 10, and we've got another parameter called type
which is set to the word boolean.
So you are going to have to think back to what you learned in yesterday's
lessons
and use the requests module to make a request to this API and passing in the
required parameters.
And then once you've generated the data from that API,
then I want you to make sure that you reach into the data that you get back and
grab the data that is in this format.
And we're going to replace this question_data with the actual questions that we
get from the open trivia database API.
When we make the request to the API in our browser,
we get back something that looks like this.
So we have a two item dictionary; one is the response code,
and one is the results.
Now the value for this key results is the list of dictionary objects that we
actually want and it looks very similar in structure to this.
So that is what we want. So everything inside here, basically.
Now we're going to still call it question_data because elsewhere in our main.py
we're tapping into this data here.
So make sure you name it the same.
And once you've done this successfully,
then you should be able to comment out all of this code here,
1: right?
0: And then simply run the program from main.py without any changes
and it should still work
but this time it should be using the questions and answers that you fetched from
the API. This is a challenge for you. So I want you to pause the video,
have a think about how you might do that and complete the challenge.