1 00:00:00,570 --> 00:00:05,570 To begin, head over to the course resources as always and download the starting 2 00:00:06,060 --> 00:00:10,200 zip file for today's starting project. And once you've done that, 3 00:00:10,200 --> 00:00:14,910 go ahead and unzip the file and open it up inside PyCharm. Now, 4 00:00:14,940 --> 00:00:17,790 if we take a look inside the project folder, 5 00:00:17,910 --> 00:00:20,430 you can see we've got a bunch of images 6 00:00:20,730 --> 00:00:23,550 which is going to be used for the buttons, 7 00:00:24,060 --> 00:00:29,060 and we've also got a lot of the code that we had created in our previous quiz 8 00:00:30,540 --> 00:00:34,650 project. It may seem like it was a long time ago, 9 00:00:34,770 --> 00:00:36,720 but back in day 17 10 00:00:36,990 --> 00:00:41,990 we learned about creating classes and the project that we built to do that was a 11 00:00:42,570 --> 00:00:45,270 text based quiz. And in fact, 12 00:00:45,300 --> 00:00:49,740 once you've downloaded and opened up the starting file for today's project, 13 00:00:50,010 --> 00:00:52,080 you can actually run this program. 14 00:00:52,470 --> 00:00:55,500 So open up main.py and run it. 15 00:00:55,770 --> 00:01:00,770 Then you can see the original format of the quiz application in action 16 00:01:01,590 --> 00:01:06,000 inside the console. We can type true or false, 17 00:01:07,170 --> 00:01:10,530 and it will give us some feedback, whether if we got it right 18 00:01:10,560 --> 00:01:13,950 or whether if we got it wrong and also our current score. 19 00:01:16,020 --> 00:01:18,720 Now this was great on day 17, 20 00:01:18,810 --> 00:01:23,490 but now that we know all about creating graphical user interfaces using tkinter 21 00:01:23,970 --> 00:01:27,840 as well as a whole bunch of things that we've learned about APIs just now, we're 22 00:01:27,840 --> 00:01:31,560 going to upgrade this quiz app to the next level. 23 00:01:32,700 --> 00:01:35,610 The first thing we want to upgrade is at the moment, 24 00:01:35,640 --> 00:01:37,920 these questions are hard-coded. 25 00:01:38,220 --> 00:01:40,920 So inside our data.py, 26 00:01:41,220 --> 00:01:46,220 you can see all of these questions as a list of dictionary objects. 27 00:01:46,800 --> 00:01:48,660 And at the time, 28 00:01:48,960 --> 00:01:52,590 the place where we got all of these questions from was in fact, 29 00:01:52,620 --> 00:01:54,510 the open trivia database. 30 00:01:54,900 --> 00:01:59,010 But what we did here is we selected the number of questions we wanted, 31 00:01:59,340 --> 00:02:01,890 we selected the category that we wanted, 32 00:02:02,220 --> 00:02:07,170 and then we changed the type to true and false and we said, generate API URL. 33 00:02:07,590 --> 00:02:09,780 And then once that URL is generated, 34 00:02:10,080 --> 00:02:15,080 all we did was we put it into our browser and we got this data and we put it 35 00:02:15,810 --> 00:02:18,960 into our question data. Now, 36 00:02:18,990 --> 00:02:22,710 this, being hard-coded, is obviously never going to change. 37 00:02:23,220 --> 00:02:28,220 So what we want to do instead is we want to get 10 questions from the open 38 00:02:29,040 --> 00:02:30,090 trivia database, 39 00:02:30,720 --> 00:02:35,720 but this time we want to make a request to the API so that we always get 40 00:02:36,480 --> 00:02:39,360 different questions and different answers. 41 00:02:40,710 --> 00:02:45,710 The idea of this challenge is to modify the code in the data.py file to get 10 42 00:02:46,380 --> 00:02:51,210 new questions using the open trivia database every time the program is run. 43 00:02:51,750 --> 00:02:55,140 Here's what your code should do after you've successfully completed the 44 00:02:55,140 --> 00:02:59,860 challenge. You'll still get a console based quiz, but this time, 45 00:02:59,890 --> 00:03:04,890 the text and the correct answer came from the open trivia database via an API 46 00:03:05,470 --> 00:03:06,303 call. 47 00:03:06,310 --> 00:03:10,120 You'll see that any symbol like single double quotes will not be displayed 48 00:03:10,120 --> 00:03:11,230 correctly just yet, 49 00:03:11,500 --> 00:03:15,520 but getting the API call working is the first step and the goal of this 50 00:03:15,520 --> 00:03:16,353 challenge. 51 00:03:17,050 --> 00:03:22,050 So what I want you to do is to set the number of questions to 10 and leave the 52 00:03:22,330 --> 00:03:25,480 category as blank, leave the difficulty is blank, 53 00:03:25,780 --> 00:03:30,780 but make sure that the type is set to true-false and then generate this API URL. 54 00:03:32,830 --> 00:03:37,830 Now remember what I said before that the API end point is everything before the 55 00:03:38,920 --> 00:03:43,360 question mark. So this is the base URL that we're going to be working with. 56 00:03:43,810 --> 00:03:47,500 And then after the question mark, we've got our different parameters. 57 00:03:47,890 --> 00:03:50,290 So we've got a parameter called amount 58 00:03:50,350 --> 00:03:55,030 which is set to 10, and we've got another parameter called type 59 00:03:55,270 --> 00:03:57,340 which is set to the word boolean. 60 00:03:57,940 --> 00:04:01,270 So you are going to have to think back to what you learned in yesterday's 61 00:04:01,270 --> 00:04:02,103 lessons 62 00:04:02,320 --> 00:04:07,320 and use the requests module to make a request to this API and passing in the 63 00:04:09,940 --> 00:04:11,740 required parameters. 64 00:04:12,250 --> 00:04:16,240 And then once you've generated the data from that API, 65 00:04:16,660 --> 00:04:21,660 then I want you to make sure that you reach into the data that you get back and 66 00:04:21,760 --> 00:04:24,970 grab the data that is in this format. 67 00:04:25,690 --> 00:04:30,310 And we're going to replace this question_data with the actual questions that we 68 00:04:30,310 --> 00:04:33,520 get from the open trivia database API. 69 00:04:34,060 --> 00:04:38,020 When we make the request to the API in our browser, 70 00:04:38,410 --> 00:04:41,140 we get back something that looks like this. 71 00:04:41,560 --> 00:04:45,610 So we have a two item dictionary; one is the response code, 72 00:04:45,940 --> 00:04:47,590 and one is the results. 73 00:04:47,920 --> 00:04:52,920 Now the value for this key results is the list of dictionary objects that we 74 00:04:54,310 --> 00:04:58,540 actually want and it looks very similar in structure to this. 75 00:04:58,930 --> 00:05:03,220 So that is what we want. So everything inside here, basically. 76 00:05:03,970 --> 00:05:08,950 Now we're going to still call it question_data because elsewhere in our main.py 77 00:05:08,950 --> 00:05:11,680 we're tapping into this data here. 78 00:05:12,160 --> 00:05:13,990 So make sure you name it the same. 79 00:05:14,170 --> 00:05:17,410 And once you've done this successfully, 80 00:05:17,710 --> 00:05:21,370 then you should be able to comment out all of this code here, 81 00:05:24,480 --> 00:05:25,050 right? 82 00:05:25,050 --> 00:05:30,050 And then simply run the program from main.py without any changes 83 00:05:30,840 --> 00:05:32,220 and it should still work 84 00:05:32,430 --> 00:05:37,350 but this time it should be using the questions and answers that you fetched from 85 00:05:37,350 --> 00:05:42,030 the API. This is a challenge for you. So I want you to pause the video, 86 00:05:42,090 --> 00:05:46,080 have a think about how you might do that and complete the challenge.