WEBVTT

0
00:00.120 --> 00:03.360
So I'm sure you've had a few gos with your quiz now

1
00:03.360 --> 00:06.930
and you've answered all the questions and you know all the correct answers.

2
00:07.440 --> 00:11.670
It's a little bit dry if we only have one question bank, right?

3
00:12.210 --> 00:15.660
Wouldn't it be nice to be able to switch out the question data with some new

4
00:15.660 --> 00:19.530
questions? Now you could certainly write your own questions,

5
00:19.560 --> 00:24.000
just change each of the texts and the answers to whatever question you want to

6
00:24.000 --> 00:28.740
add. An alternative though, is to use the Open Trivia database,

7
00:28.920 --> 00:33.480
which is a free-to-use user contributed trivia question database

8
00:33.900 --> 00:38.900
and it has over 3000 verified questions to pick from. In the course resources

9
00:39.300 --> 00:44.190
I've got a link to the open TDB, the open trivia database,

10
00:44.610 --> 00:49.610
and we can go ahead and take a look at their API to see how we can generate some

11
00:49.710 --> 00:54.120
questions. For example, if I wanted 10 questions on,

12
00:54.480 --> 00:59.430
let's say computers and I want the difficulty to be easy

13
00:59.880 --> 01:02.850
and I want to select the type to be true or false

14
01:03.270 --> 01:06.840
And then I'm going to go and click generate API URL.

15
01:07.420 --> 01:08.940
Now that I've got my URL,

16
01:08.940 --> 01:12.600
I can open up a new tab and go to that location.

17
01:13.320 --> 01:18.320
Now, we'll get some data generated in a JSON format

18
01:18.990 --> 01:21.750
which is a JavaScript object notation

19
01:22.170 --> 01:26.700
but it actually looks pretty much the same as a Python dictionary.

20
01:27.210 --> 01:32.210
So let's go ahead and copy everything that's here and then go into our data.py

21
01:33.570 --> 01:38.570
and I'm going to replace the entire question data with what I got just now.

22
01:39.330 --> 01:42.870
Now once you paste it in, it's going to be very hard to read.

23
01:42.930 --> 01:47.930
So we'll need to go to code and then reformat code in order to get it to look a

24
01:48.870 --> 01:50.160
little bit more normal.

25
01:50.610 --> 01:55.610
So you can see that we've got this dictionary and it has two key-value pairs.

26
01:57.480 --> 02:02.480
One is a response code and another is a list which is under the key results.

27
02:04.350 --> 02:06.060
If I go and reformat this again,

28
02:06.090 --> 02:10.950
you can see that results is comprised of 10 dictionaries.

29
02:11.490 --> 02:11.700
Now,

30
02:11.700 --> 02:16.700
each of these dictionaries contain a whole bunch of key-value pairs and inside

31
02:17.430 --> 02:20.760
each of these key-value pairs it has a bunch of things

32
02:20.760 --> 02:24.960
including the category, that type, the difficulty,

33
02:24.960 --> 02:29.370
the question, the correct answer, and the incorrect answers.

34
02:29.850 --> 02:34.850
So what we can see here is we have a dictionary and then we have these two key

35
02:35.820 --> 02:36.653
value pairs.

36
02:37.110 --> 02:42.110
So you can simply just get rid of the enclosing dictionary and leave it so that

37
02:43.080 --> 02:47.940
you have this as a list of dictionaries. Now,

38
02:47.970 --> 02:52.970
if we go and reformat our code so that we can see each of these dictionary

39
02:53.310 --> 02:54.143
objects,

40
02:54.390 --> 02:59.070
you can see there's a total of five key value pairs and they each have the keys

41
02:59.080 --> 03:03.970
category, type, question, which is the question text and the correct answer

42
03:04.000 --> 03:06.550
which is a string that's true or false.

43
03:07.120 --> 03:10.360
These are the two bits of information that we're interested in.

44
03:10.840 --> 03:15.840
So can you figure out how you can modify the main.py in order to get our

45
03:16.030 --> 03:20.320
quiz to start working again with this new data?

46
03:20.830 --> 03:23.110
Pause the video and complete this challenge.

47
03:26.400 --> 03:30.810
All right. So we've got our question data from the data file,

48
03:31.320 --> 03:35.010
but now we have to change the names of these keys.

49
03:35.460 --> 03:37.890
Whereas before it was called text and answer,

50
03:38.310 --> 03:41.340
now it's called question and correct answer.

51
03:41.520 --> 03:46.520
So the question_text is under the key question and correct answer is under

52
03:46.830 --> 03:48.330
the key correct _answer.

53
03:48.840 --> 03:53.250
So just by changing those two things and pasting all of that data in

54
03:53.640 --> 03:58.640
we can already get started with this brand new quiz. And you can play with it and

55
04:00.780 --> 04:04.620
mess around with different data from the open trivia database,

56
04:04.860 --> 04:07.380
change the category, change the difficulty,

57
04:07.710 --> 04:10.770
and you have endless number of questions to play with.

58
04:11.460 --> 04:16.260
And this really brings about some of the advantages of Object Oriented

59
04:16.260 --> 04:21.260
Programming. Notice how only our main.py file actually has knowledge of how each

60
04:23.550 --> 04:28.550
of these classes work and behave. Our QuizBrain actually didn't need to be

61
04:30.000 --> 04:31.470
touched at all

62
04:31.500 --> 04:36.150
when we changed our data over. This is modularity at its best.

63
04:36.450 --> 04:41.450
We're able to completely switch up the question data to a different language,

64
04:41.670 --> 04:46.200
to a different topic, to a different format, and the quiz brain doesn't care.

65
04:46.680 --> 04:51.680
All it has to concern itself with is how to track which question we're on,

66
04:52.080 --> 04:55.230
how to get the next question, how to check the answer.

67
04:55.650 --> 04:59.940
And as long as it's able to do that and perform the functionality of a quiz,

68
05:00.330 --> 05:04.440
it's unconcerned by where the data comes from, how it's formatted,

69
05:04.770 --> 05:08.430
and it will continue to work. So, as you can imagine,

70
05:08.460 --> 05:10.770
if your colleague was working on quiz brain,

71
05:11.190 --> 05:15.450
they don't actually need to know how the data looks or how it's structured.

72
05:15.840 --> 05:19.170
And you, on the other hand, could be working on the data,

73
05:19.200 --> 05:23.070
getting hold of each of these pieces of data or writing the quiz.

74
05:23.550 --> 05:26.910
And you can already see how we've got a piece of program

75
05:27.210 --> 05:32.210
that's able to be far more complex than what we used to be able to create with

76
05:32.580 --> 05:34.050
simple procedural code.