1
00:00:00,390 --> 00:00:04,230
Now that we've seen how to create our own custom classes,

2
00:00:04,650 --> 00:00:06,990
we're finally ready to build the quiz game.

3
00:00:07,680 --> 00:00:10,710
Now this game is going to be a simple true/false game.

4
00:00:11,010 --> 00:00:15,000
So a question will be asked to the user and they'll select true or false,

5
00:00:15,420 --> 00:00:19,080
and then we'll tell them if they got it right or wrong and give them a score.

6
00:00:20,040 --> 00:00:24,270
Now to begin, the first step you'll need to do is to download the zip

7
00:00:24,270 --> 00:00:27,570
file it with all the starting code from our course resource website.

8
00:00:28,020 --> 00:00:28,920
Alternatively,

9
00:00:28,950 --> 00:00:33,030
if you are already logged in on Repl.it simply fork the project and then click

10
00:00:33,030 --> 00:00:35,550
on these three dots to download the zip file here.

11
00:00:36,720 --> 00:00:40,050
And once you've extracted the actual folder, quiz-game-

12
00:00:40,050 --> 00:00:44,520
start from the.zip file, then we're ready to go ahead and open it

13
00:00:44,670 --> 00:00:47,610
using PyCharm. In PyCharm 

14
00:00:47,640 --> 00:00:51,960
we're going to click open and then we're going to navigate to the place where we

15
00:00:51,960 --> 00:00:56,460
have our quiz-game-start, which in my case is in the downloads folder.

16
00:00:57,390 --> 00:01:01,080
PyCharm should automatically configure the latest version of Python you've got

17
00:01:01,080 --> 00:01:03,120
installed as the interpreter.

18
00:01:03,480 --> 00:01:06,150
So you can go ahead and close these popups.

19
00:01:06,630 --> 00:01:09,480
And if we open up our quiz-game-start folder,

20
00:01:09,720 --> 00:01:13,110
you can see we have four files, data,

21
00:01:13,140 --> 00:01:15,450
main, question_model and quiz_brain.

22
00:01:16,050 --> 00:01:18,240
And over the course of the next few lessons,

23
00:01:18,300 --> 00:01:22,290
we're going to be writing the code in all of these files so that we end up

24
00:01:22,320 --> 00:01:24,600
creating our final quiz project.

25
00:01:25,650 --> 00:01:30,650
The first task that we have is to create a model for a question in our quiz.

26
00:01:32,280 --> 00:01:37,280
If we had a question object, well what kind of attributes should it have?

27
00:01:37,860 --> 00:01:42,030
It might have a text attribute for the question texts,

28
00:01:42,360 --> 00:01:44,940
and it might also hold an answer attribute

29
00:01:45,090 --> 00:01:49,260
which is going to be the correct answer to that question. Now,

30
00:01:49,260 --> 00:01:53,340
these two attributes should be initialized with a value

31
00:01:53,640 --> 00:01:57,720
when a new question object is created from this class.

32
00:01:58,230 --> 00:02:02,040
For example, if our first question was 2 + 3 = 5,

33
00:02:02,340 --> 00:02:05,340
which we know the correct answer should be true. Well,

34
00:02:05,340 --> 00:02:10,139
then we might write the code like this to actually initialize a new question

35
00:02:10,139 --> 00:02:15,140
object. And when this initialization goes through the constructor code inside the

36
00:02:16,890 --> 00:02:17,820
question class

37
00:02:18,120 --> 00:02:23,120
will take these two pieces of data and then add them to the corresponding

38
00:02:23,220 --> 00:02:25,470
attributes, the text and the answer.

39
00:02:26,070 --> 00:02:31,070
And so our new question object will have both of these attributes initialized

40
00:02:31,470 --> 00:02:32,490
with a value

41
00:02:32,760 --> 00:02:37,760
when a new question object is being created. Inside your project,

42
00:02:39,030 --> 00:02:42,660
go over to the question_model.py file,

43
00:02:43,110 --> 00:02:48,030
and go ahead and create a new class called question

44
00:02:48,510 --> 00:02:51,960
and that question class should have an init method

45
00:02:52,200 --> 00:02:55,170
which will initialize two attributes,

46
00:02:55,440 --> 00:02:59,950
the text and the answer just as you've seen in the previous slides.

47
00:03:00,850 --> 00:03:04,600
Pause the video and see if you can complete this challenge by creating this

48
00:03:04,600 --> 00:03:05,130
class.

49
00:03:05,130 --> 00:03:05,963
Right.

50
00:03:10,590 --> 00:03:12,600
All right. So we know that to create a class,

51
00:03:12,600 --> 00:03:16,260
we first have to use the class keyword. And then in our case,

52
00:03:16,290 --> 00:03:20,700
the name of the class is going to be called Question because we're modeling what

53
00:03:20,700 --> 00:03:23,160
a question would be like in our game.

54
00:03:23,940 --> 00:03:27,270
We know that each of these questions have two attributes;

55
00:03:27,690 --> 00:03:30,240
a self.text and a self.answer.

56
00:03:30,840 --> 00:03:34,650
And those two attributes are going to be initialized

57
00:03:35,400 --> 00:03:40,400
when we create a new question object. So we need the init function in here.

58
00:03:41,520 --> 00:03:43,770
And we're pretty much going to use the autofill.

59
00:03:43,830 --> 00:03:46,980
So once you write Def and then you write init,

60
00:03:47,280 --> 00:03:50,610
you can see it in the dropdown list and then just hit enter

61
00:03:50,970 --> 00:03:54,240
and the code will be inserted automatically. This way

62
00:03:54,240 --> 00:03:58,050
you don't have to remember the exact order of the parentheses or where to put

63
00:03:58,050 --> 00:04:02,340
the colon, et cetera, et cetera. Now, inside the init function,

64
00:04:02,370 --> 00:04:05,580
we're going to set up two attributes as we said.

65
00:04:05,910 --> 00:04:09,660
One is going to be called text and one is going to be called answer.

66
00:04:10,290 --> 00:04:13,980
And if we want to create an attribute, we have to use this syntax,

67
00:04:14,050 --> 00:04:17,040
self. and then the name of the attribute,

68
00:04:17,370 --> 00:04:21,120
because eventually when we create a new object from this class

69
00:04:21,390 --> 00:04:26,390
say a new question object, then we're going to be passing in these two items,

70
00:04:27,510 --> 00:04:30,270
a piece of text which is going to be the question,

71
00:04:30,510 --> 00:04:35,400
and then either true or false as the answer. And then later on

72
00:04:35,400 --> 00:04:37,320
if we wanted to access the text,

73
00:04:37,380 --> 00:04:40,590
then we would say new_q.text.

74
00:04:41,190 --> 00:04:42,720
Now of course at the moment,

75
00:04:42,750 --> 00:04:46,680
these are all yellowed out because it seems to have no effect.

76
00:04:47,010 --> 00:04:51,510
There's no way of receiving a value for each of these attributes.

77
00:04:51,990 --> 00:04:56,010
And to do that, we have to add some inputs inside the init function.

78
00:04:56,490 --> 00:04:59,730
So let's just call it q_text and a q_answer.

79
00:05:00,090 --> 00:05:02,910
You can of course call these parameters anything you want.

80
00:05:03,330 --> 00:05:08,330
But the important part is you set the self.text to that first value and the

81
00:05:09,960 --> 00:05:12,450
self.answer to the second value.

82
00:05:13,020 --> 00:05:18,020
So now when we actually go ahead and print this new_q.text

83
00:05:18,840 --> 00:05:20,730
object like we have here,

84
00:05:20,820 --> 00:05:24,750
then it will go ahead and actually fetch this value into the output.

85
00:05:26,250 --> 00:05:30,810
If you go to run and then run and then select a question model,

86
00:05:31,380 --> 00:05:35,400
you can see that is what gets printed in the console.

87
00:05:37,110 --> 00:05:41,940
Let's delete those two lines and now we have completed our first task,

88
00:05:42,270 --> 00:05:46,200
which is creating our question class from scratch.

