1
00:00:00,120 --> 00:00:03,360
So I'm sure you've had a few gos with your quiz now

2
00:00:03,360 --> 00:00:06,930
and you've answered all the questions and you know all the correct answers.

3
00:00:07,440 --> 00:00:11,670
It's a little bit dry if we only have one question bank, right?

4
00:00:12,210 --> 00:00:15,660
Wouldn't it be nice to be able to switch out the question data with some new

5
00:00:15,660 --> 00:00:19,530
questions? Now you could certainly write your own questions,

6
00:00:19,560 --> 00:00:24,000
just change each of the texts and the answers to whatever question you want to

7
00:00:24,000 --> 00:00:28,740
add. An alternative though, is to use the Open Trivia database,

8
00:00:28,920 --> 00:00:33,480
which is a free-to-use user contributed trivia question database

9
00:00:33,900 --> 00:00:38,900
and it has over 3000 verified questions to pick from. In the course resources

10
00:00:39,300 --> 00:00:44,190
I've got a link to the open TDB, the open trivia database,

11
00:00:44,610 --> 00:00:49,610
and we can go ahead and take a look at their API to see how we can generate some

12
00:00:49,710 --> 00:00:54,120
questions. For example, if I wanted 10 questions on,

13
00:00:54,480 --> 00:00:59,430
let's say computers and I want the difficulty to be easy

14
00:00:59,880 --> 00:01:02,850
and I want to select the type to be true or false

15
00:01:03,270 --> 00:01:06,840
And then I'm going to go and click generate API URL.

16
00:01:07,420 --> 00:01:08,940
Now that I've got my URL,

17
00:01:08,940 --> 00:01:12,600
I can open up a new tab and go to that location.

18
00:01:13,320 --> 00:01:18,320
Now, we'll get some data generated in a JSON format

19
00:01:18,990 --> 00:01:21,750
which is a JavaScript object notation

20
00:01:22,170 --> 00:01:26,700
but it actually looks pretty much the same as a Python dictionary.

21
00:01:27,210 --> 00:01:32,210
So let's go ahead and copy everything that's here and then go into our data.py

22
00:01:33,570 --> 00:01:38,570
and I'm going to replace the entire question data with what I got just now.

23
00:01:39,330 --> 00:01:42,870
Now once you paste it in, it's going to be very hard to read.

24
00:01:42,930 --> 00:01:47,930
So we'll need to go to code and then reformat code in order to get it to look a

25
00:01:48,870 --> 00:01:50,160
little bit more normal.

26
00:01:50,610 --> 00:01:55,610
So you can see that we've got this dictionary and it has two key-value pairs.

27
00:01:57,480 --> 00:02:02,480
One is a response code and another is a list which is under the key results.

28
00:02:04,350 --> 00:02:06,060
If I go and reformat this again,

29
00:02:06,090 --> 00:02:10,949
you can see that results is comprised of 10 dictionaries.

30
00:02:11,490 --> 00:02:11,700
Now,

31
00:02:11,700 --> 00:02:16,700
each of these dictionaries contain a whole bunch of key-value pairs and inside

32
00:02:17,430 --> 00:02:20,760
each of these key-value pairs it has a bunch of things

33
00:02:20,760 --> 00:02:24,960
including the category, that type, the difficulty,

34
00:02:24,960 --> 00:02:29,370
the question, the correct answer, and the incorrect answers.

35
00:02:29,850 --> 00:02:34,850
So what we can see here is we have a dictionary and then we have these two key

36
00:02:35,820 --> 00:02:36,653
value pairs.

37
00:02:37,110 --> 00:02:42,110
So you can simply just get rid of the enclosing dictionary and leave it so that

38
00:02:43,080 --> 00:02:47,940
you have this as a list of dictionaries. Now,

39
00:02:47,970 --> 00:02:52,970
if we go and reformat our code so that we can see each of these dictionary

40
00:02:53,310 --> 00:02:54,143
objects,

41
00:02:54,390 --> 00:02:59,070
you can see there's a total of five key value pairs and they each have the keys

42
00:02:59,080 --> 00:03:03,970
category, type, question, which is the question text and the correct answer

43
00:03:04,000 --> 00:03:06,550
which is a string that's true or false.

44
00:03:07,120 --> 00:03:10,360
These are the two bits of information that we're interested in.

45
00:03:10,840 --> 00:03:15,840
So can you figure out how you can modify the main.py in order to get our

46
00:03:16,030 --> 00:03:20,320
quiz to start working again with this new data?

47
00:03:20,830 --> 00:03:23,110
Pause the video and complete this challenge.

48
00:03:26,400 --> 00:03:30,810
All right. So we've got our question data from the data file,

49
00:03:31,320 --> 00:03:35,010
but now we have to change the names of these keys.

50
00:03:35,460 --> 00:03:37,890
Whereas before it was called text and answer,

51
00:03:38,310 --> 00:03:41,340
now it's called question and correct answer.

52
00:03:41,520 --> 00:03:46,520
So the question_text is under the key question and correct answer is under

53
00:03:46,830 --> 00:03:48,330
the key correct _answer.

54
00:03:48,840 --> 00:03:53,250
So just by changing those two things and pasting all of that data in

55
00:03:53,640 --> 00:03:58,640
we can already get started with this brand new quiz. And you can play with it and

56
00:04:00,780 --> 00:04:04,620
mess around with different data from the open trivia database,

57
00:04:04,860 --> 00:04:07,380
change the category, change the difficulty,

58
00:04:07,710 --> 00:04:10,770
and you have endless number of questions to play with.

59
00:04:11,460 --> 00:04:16,260
And this really brings about some of the advantages of Object Oriented

60
00:04:16,260 --> 00:04:21,260
Programming. Notice how only our main.py file actually has knowledge of how each

61
00:04:23,550 --> 00:04:28,550
of these classes work and behave. Our QuizBrain actually didn't need to be

62
00:04:30,000 --> 00:04:31,470
touched at all

63
00:04:31,500 --> 00:04:36,150
when we changed our data over. This is modularity at its best.

64
00:04:36,450 --> 00:04:41,450
We're able to completely switch up the question data to a different language,

65
00:04:41,670 --> 00:04:46,200
to a different topic, to a different format, and the quiz brain doesn't care.

66
00:04:46,680 --> 00:04:51,680
All it has to concern itself with is how to track which question we're on,

67
00:04:52,080 --> 00:04:55,230
how to get the next question, how to check the answer.

68
00:04:55,650 --> 00:04:59,940
And as long as it's able to do that and perform the functionality of a quiz,

69
00:05:00,330 --> 00:05:04,440
it's unconcerned by where the data comes from, how it's formatted,

70
00:05:04,770 --> 00:05:08,430
and it will continue to work. So, as you can imagine,

71
00:05:08,460 --> 00:05:10,770
if your colleague was working on quiz brain,

72
00:05:11,190 --> 00:05:15,450
they don't actually need to know how the data looks or how it's structured.

73
00:05:15,840 --> 00:05:19,170
And you, on the other hand, could be working on the data,

74
00:05:19,200 --> 00:05:23,070
getting hold of each of these pieces of data or writing the quiz.

75
00:05:23,550 --> 00:05:26,910
And you can already see how we've got a piece of program

76
00:05:27,210 --> 00:05:32,210
that's able to be far more complex than what we used to be able to create with

77
00:05:32,580 --> 00:05:34,050
simple procedural code.

