WEBVTT

0
00:00.200 --> 00:07.190
Now that we've seen how to create our own custom classes, we're finally ready to build the Quiz Game.

1
00:07.400 --> 00:10.790
Now this game is going to be a simple, True / False game,

2
00:10.790 --> 00:15.170
so a question will be asked to the user and they'll select True or False,

3
00:15.170 --> 00:19.250
and then we'll tell them if they got it right or wrong and give them a score.

4
00:19.730 --> 00:25.850
Now to begin, the first step you'll need to do is to download the zip file with all the starting code

5
00:25.850 --> 00:27.830
from our Course Resource website.

6
00:27.830 --> 00:33.830
And once you've extracted the actual folder, quiz-game-start from the .zip file,

7
00:33.830 --> 00:37.490
then we're ready to go ahead and open it using PyCharm.

8
00:38.120 --> 00:40.490
In PyCharm we're going to click Open,

9
00:40.490 --> 00:46.760
and then we're going to navigate to the place where we have our quiz-game-start, which in my case is

10
00:46.760 --> 00:48.140
in the Downloads folder.

11
00:48.680 --> 00:54.650
PyCharm should automatically configure the latest version of Python you've got installed as the interpreter.

12
00:54.650 --> 00:57.740
So you can go ahead and close these pop-ups.

13
00:57.740 --> 01:06.020
And if we open up our quiz-game-start folder, you can see we have four files, data, main, question_model,

14
01:06.020 --> 01:07.100
and quiz_brain.

15
01:07.100 --> 01:12.650
And over the course of the next few lessons, we're going to be writing the code in all of these files

16
01:12.650 --> 01:16.160
so that we end up creating our Final Quiz Project.

17
01:16.820 --> 01:23.350
The first task that we have is to create a model for a question in our quiz.

18
01:23.500 --> 01:28.990
If we had a Question object, well, what kind of attributes should it have?

19
01:29.020 --> 01:36.340
It might have a text attribute for the question text, and it might also hold an answer attribute,

20
01:36.340 --> 01:39.730
which is going to be the correct answer to that question.

21
01:40.240 --> 01:48.400
Now these two attributes should be initialized with a value when a new_q object is created from

22
01:48.400 --> 01:49.300
this class.

23
01:49.360 --> 01:55.360
So for example, if our first question was 2 + 3 = 5, which we know the correct answer

24
01:55.360 --> 01:56.380
should be True,

25
01:56.410 --> 02:02.320
well, then we might write the code like this to actually initialize a new_q object.

26
02:02.320 --> 02:10.480
And when this initialization goes through, the constructor code inside the Question class will take these

27
02:10.480 --> 02:17.170
two pieces of data and then add them to the corresponding attributes, the text and the answer.

28
02:17.170 --> 02:23.980
And so our new_q object will have both of these attributes initialized with a value

29
02:23.980 --> 02:34.240
when a new_q object is being created. Inside your project, go over to the question_model.py file,

30
02:34.240 --> 02:39.580
and go ahead and create a new class called Question,

31
02:39.580 --> 02:48.250
and that Question class should have an __init()__ method which will initialize two attributes the text and

32
02:48.250 --> 02:51.640
the answer, just as you've seen in the previous slides.

33
02:52.000 --> 02:56.680
Pause the video and see if you can complete this challenge by creating this class.

34
03:01.810 --> 03:02.350
All right.

35
03:02.350 --> 03:06.300
So we know that to create a class we first have to use the class keyword.

36
03:06.300 --> 03:12.120
And then in our case the name of the class is going to be called Question because we're modeling what

37
03:12.120 --> 03:14.820
a question would be like in our game.

38
03:15.120 --> 03:21.870
We know that each of these questions have two attributes a self.text and a self.answer,

39
03:21.870 --> 03:29.970
and those two attributes are going to be initialized when we create a new_q object,

40
03:29.970 --> 03:32.610
so we need the __init()__ function in here.

41
03:32.610 --> 03:35.130
And we're pretty much going to use the autofill,

42
03:35.130 --> 03:40.860
so once you write 'def', and then you write 'init', you can see it in the dropdown list,

43
03:40.860 --> 03:44.970
and then just hit Enter, and the code will be inserted automatically.

44
03:44.970 --> 03:50.850
This way you don't have to remember the exact order of the parentheses, or where to put the colon, et cetera.

45
03:50.850 --> 03:51.600
et cetera.

46
03:51.630 --> 03:56.280
Now inside the __init()__ function, we're going to set up two attributes

47
03:56.280 --> 04:01.350
s we said, one is going to be called text and one is going to be called answer.

48
04:01.350 --> 04:08.550
And if we want to create an attribute we have to use this syntax self. and then the name of the attribute,

49
04:08.550 --> 04:15.690
because eventually when we create a new object from this class, say a new_q object, then we're

50
04:15.690 --> 04:22.290
going to be passing in these two items a piece of text, which is going to be the question, and then

51
04:22.290 --> 04:24.930
either True or False as the answer.

52
04:24.930 --> 04:32.220
And then later on, if we wanted to access the text, then we would say new_q.text.

53
04:32.250 --> 04:38.220
Now of course, at the moment these are all yellowed out because it seems to have no effect.

54
04:38.220 --> 04:43.110
There's no way of receiving a value for each of these attributes.

55
04:43.110 --> 04:47.640
And to do that we have to add some inputs inside the init function.

56
04:47.640 --> 04:51.240
So let's just call it q_text and q_answer.

57
04:51.240 --> 04:57.990
You can of course call these parameters anything you want, but the important part is you set the self.text

58
04:57.990 --> 05:04.110
to that first value, and the self.answer to the second value.

59
05:04.110 --> 05:12.500
So now when we actually go ahead and print this new_q.text object, like we have here, then

60
05:12.500 --> 05:16.310
it will go ahead and actually fetch this value into the output.

61
05:17.450 --> 05:25.490
If you go to Run and then Run and then select question_model, you can see that is what gets printed

62
05:25.490 --> 05:27.020
in the console.

63
05:28.280 --> 05:29.840
Let's delete those two lines.

64
05:29.840 --> 05:37.730
And now we have completed our first task which is creating our Question class from scratch.