WEBVTT

0
00:00.570 --> 00:05.570
To begin, head over to the course resources as always and download the starting 

1
00:06.060 --> 00:10.200
zip file for today's starting project. And once you've done that,

2
00:10.200 --> 00:14.910
go ahead and unzip the file and open it up inside PyCharm. Now,

3
00:14.940 --> 00:17.790
if we take a look inside the project folder,

4
00:17.910 --> 00:20.430
you can see we've got a bunch of images

5
00:20.730 --> 00:23.550
which is going to be used for the buttons,

6
00:24.060 --> 00:29.060
and we've also got a lot of the code that we had created in our previous quiz

7
00:30.540 --> 00:34.650
project. It may seem like it was a long time ago,

8
00:34.770 --> 00:36.720
but back in day 17

9
00:36.990 --> 00:41.990
we learned about creating classes and the project that we built to do that was a

10
00:42.570 --> 00:45.270
text based quiz. And in fact,

11
00:45.300 --> 00:49.740
once you've downloaded and opened up the starting file for today's project,

12
00:50.010 --> 00:52.080
you can actually run this program.

13
00:52.470 --> 00:55.500
So open up main.py and run it.

14
00:55.770 --> 01:00.770
Then you can see the original format of the quiz application in action

15
01:01.590 --> 01:06.000
inside the console. We can type true or false,

16
01:07.170 --> 01:10.530
and it will give us some feedback, whether if we got it right

17
01:10.560 --> 01:13.950
or whether if we got it wrong and also our current score.

18
01:16.020 --> 01:18.720
Now this was great on day 17,

19
01:18.810 --> 01:23.490
but now that we know all about creating graphical user interfaces using tkinter

20
01:23.970 --> 01:27.840
as well as a whole bunch of things that we've learned about APIs just now, we're

21
01:27.840 --> 01:31.560
going to upgrade this quiz app to the next level.

22
01:32.700 --> 01:35.610
The first thing we want to upgrade is at the moment,

23
01:35.640 --> 01:37.920
these questions are hard-coded.

24
01:38.220 --> 01:40.920
So inside our data.py,

25
01:41.220 --> 01:46.220
you can see all of these questions as a list of dictionary objects.

26
01:46.800 --> 01:48.660
And at the time,

27
01:48.960 --> 01:52.590
the place where we got all of these questions from was in fact,

28
01:52.620 --> 01:54.510
the open trivia database.

29
01:54.900 --> 01:59.010
But what we did here is we selected the number of questions we wanted,

30
01:59.340 --> 02:01.890
we selected the category that we wanted,

31
02:02.220 --> 02:07.170
and then we changed the type to true and false and we said, generate API URL.

32
02:07.590 --> 02:09.780
And then once that URL is generated,

33
02:10.080 --> 02:15.080
all we did was we put it into our  browser and we got this data and we put it

34
02:15.810 --> 02:18.960
into our question data. Now,

35
02:18.990 --> 02:22.710
this, being hard-coded, is obviously never going to change.

36
02:23.220 --> 02:28.220
So what we want to do instead is we want to get 10 questions from the open

37
02:29.040 --> 02:30.090
trivia database,

38
02:30.720 --> 02:35.720
but this time we want to make a request to the API so that we always get

39
02:36.480 --> 02:39.360
different questions and different answers.

40
02:40.710 --> 02:45.710
The idea of this challenge is to modify the code in the data.py file to get 10

41
02:46.380 --> 02:51.210
new questions using the open trivia database every time the program is run.

42
02:51.750 --> 02:55.140
Here's what your code should do after you've successfully completed the

43
02:55.140 --> 02:59.860
challenge. You'll still get a console based quiz, but this time,

44
02:59.890 --> 03:04.890
the text and the correct answer came from the open trivia database via an API

45
03:05.470 --> 03:06.303
call.

46
03:06.310 --> 03:10.120
You'll see that any symbol like single double quotes will not be displayed

47
03:10.120 --> 03:11.230
correctly just yet,

48
03:11.500 --> 03:15.520
but getting the API call working is the first step and the goal of this

49
03:15.520 --> 03:16.353
challenge.

50
03:17.050 --> 03:22.050
So what I want you to do is to set the number of questions to 10 and leave the

51
03:22.330 --> 03:25.480
category as blank, leave the difficulty is blank,

52
03:25.780 --> 03:30.780
but make sure that the type is set to true-false and then generate this API URL.

53
03:32.830 --> 03:37.830
Now remember what I said before that the API end point is everything before the

54
03:38.920 --> 03:43.360
question mark. So this is the base URL that we're going to be working with.

55
03:43.810 --> 03:47.500
And then after the question mark, we've got our different parameters.

56
03:47.890 --> 03:50.290
So we've got a parameter called amount

57
03:50.350 --> 03:55.030
which is set to 10, and we've got another parameter called type

58
03:55.270 --> 03:57.340
which is set to the word boolean.

59
03:57.940 --> 04:01.270
So you are going to have to think back to what you learned in yesterday's

60
04:01.270 --> 04:02.103
lessons

61
04:02.320 --> 04:07.320
and use the requests module to make a request to this API and passing in the

62
04:09.940 --> 04:11.740
required parameters.

63
04:12.250 --> 04:16.240
And then once you've generated the data from that API,

64
04:16.660 --> 04:21.660
then I want you to make sure that you reach into the data that you get back and

65
04:21.760 --> 04:24.970
grab the data that is in this format.

66
04:25.690 --> 04:30.310
And we're going to replace this question_data with the actual questions that we

67
04:30.310 --> 04:33.520
get from the open trivia database API.

68
04:34.060 --> 04:38.020
When we make the request to the API in our browser,

69
04:38.410 --> 04:41.140
we get back something that looks like this.

70
04:41.560 --> 04:45.610
So we have a two item dictionary; one is the response code,

71
04:45.940 --> 04:47.590
and one is the results.

72
04:47.920 --> 04:52.920
Now the value for this key results is the list of dictionary objects that we

73
04:54.310 --> 04:58.540
actually want and it looks very similar in structure to this.

74
04:58.930 --> 05:03.220
So that is what we want. So everything inside here, basically.

75
05:03.970 --> 05:08.950
Now we're going to still call it question_data because elsewhere in our main.py

76
05:08.950 --> 05:11.680
we're tapping into this data here.

77
05:12.160 --> 05:13.990
So make sure you name it the same.

78
05:14.170 --> 05:17.410
And once you've done this successfully,

79
05:17.710 --> 05:21.370
then you should be able to comment out all of this code here,

80
05:24.480 --> 05:25.050
<v 1>right?</v>

81
05:25.050 --> 05:30.050
<v 0>And then simply run the program from main.py without any changes</v>

82
05:30.840 --> 05:32.220
and it should still work

83
05:32.430 --> 05:37.350
but this time it should be using the questions and answers that you fetched from

84
05:37.350 --> 05:42.030
the API. This is a challenge for you. So I want you to pause the video,

85
05:42.090 --> 05:46.080
have a think about how you might do that and complete the challenge.