Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Let's build our question classes and figure out a plan for the actual quiz.
-
0:03
[SOUND] So let's use our new found date time abilities to make
-
0:07
an app that will ask us math questions and time our answers.
-
0:11
We're gonna keep it pretty bare bones, but let us leave a place or two for
-
0:15
improvements.
-
0:15
If you haven't watched object oriented Python yet,
-
0:18
you'll probably want to before this video, so you won't be lost.
-
0:21
All right.
-
0:22
Ready?
-
0:22
Let's get started.
-
0:25
Okay. So
-
0:25
let's create a few classes that'll make it easier for us to create and ask questions.
-
0:31
Our app is just going to ask addition and multiplication questions.
-
0:35
So, our classes should be pretty simple.
-
0:38
I'm doing this in a file called questions.py.
-
0:41
So let's make a class Question and it's gonna have two attributes.
-
0:46
Answer, which is None and text, which is None.
-
0:50
I'm actually going to leave the class right there.
-
0:53
You would think I'd add more, but I want these to be very simple.
-
0:56
I want them to have a very simple API and
-
0:58
I don't want them to be crazily overextended.
-
1:01
You could definitely add some more to this to make them simpler or
-
1:04
to make them a little smarter if you want.
-
1:06
That's completely up to you.
-
1:08
So now let's make a class called Add and
-
1:11
it's going to extend question and I'm gonna override the init for it.
-
1:16
And it's going to take two numbers and so
-
1:20
we're gonna set self.text equal to this string format and
-
1:26
we're gonna stick in number one and number two.
-
1:30
And self.answer is going to be the actual value of num1 plus num2.
-
1:36
Okay.
-
1:37
So let's do the same thing here for multiply.
-
1:42
In fact, I'm going to copy this and
-
1:45
what we're going to do is we're going to change this, I'm going to put in an x.
-
1:51
You could use the time, the, the star, you could use a unicode time.
-
1:55
You can use the word times, whatever you wanna do.
-
1:58
So the only thing different here is how we calculate stuff.
-
2:02
I think this was pretty quick.
-
2:03
I think this was worth it, but let's test these out.
-
2:07
So let's come down here to our workspace and
-
2:12
let's do from questions import Add.
-
2:17
And we are going to do add1 equals Add and
-
2:20
we are going to pass int numbers, so lets do five and seven.
-
2:25
Okay.
-
2:25
If we look at add1.text, we get 5 plus 7.
-
2:29
And if we look at add1.answer, we get 12, which is right.
-
2:34
That's pretty awesome.
-
2:35
That's, that's what I wanted to do.
-
2:37
So before we move on and wrap up this video,
-
2:41
let's actually go ahead and do our quiz.
-
2:45
We'll do some planning on our quiz.
-
2:47
So New File, we'll do quiz.py.
-
2:51
At this point, well we're gonna have a class.
-
2:54
Right?
-
2:55
And we're gonna want to be able to store and
-
2:58
hold onto all the questions that we have.
-
3:00
So, our questions is gonna be an empty list.
-
3:03
Okay.
-
3:03
So cool.
-
3:04
But oh, wait.
-
3:05
So, if we're gonna use questions, we're gonna have to import them.
-
3:07
So we need to do from questions import Add, Multiply.
-
3:13
So we've got both of those.
-
3:14
We're also gonna wanna track how long it takes to answer these things, so we need
-
3:18
a start time and an end time and we're gonna wanna build these things randomly.
-
3:22
So that means, we're going to need to import datetime and import random.
-
3:27
So you notice that there's a blank line in between datetime random and questions?
-
3:32
That's because my local imports, I wanna have this, this little bit of separation,
-
3:36
just so I can see, like, okay, these are top level right here.
-
3:39
And these are my own custom ones right here and so on.
-
3:43
By top-level, I mean like built into the standard library.
-
3:46
You don't have to do this, but it's a good habit.
-
3:48
All right.
-
3:48
So when we initialize our quiz, we're gonna want to generate some question.
-
3:55
Right?
-
3:56
So let's, I don't know, we could probably do that indefinite.
-
4:00
And so let's just say, generate 10 random
-
4:05
questions with numbers from 1 to 10.
-
4:09
And then we'll say, add these questions into self.questions.
-
4:15
Okay.
-
4:15
Pretty simple.
-
4:16
Let's add a new method here called take_quiz.
-
4:21
And I think this is gonna be the method that actually, like asks the questions.
-
4:24
So, let's see.
-
4:26
Log the start time and ask all of the questions.
-
4:32
And log if they got the question right and
-
4:37
log the end time and then show a summary.
-
4:42
Okay.
-
4:43
This makes me think that since we've got this, this ask all the questions
-
4:48
it makes me think that maybe we should have a method that asks the questions.
-
4:52
So let's call it maybe ask and it'll have a question.
-
4:56
But you know what? Before we do that,
-
4:58
we'll put a pass in there, just to let these things pass.
-
5:00
Let's go up here and add an answers list.
-
5:03
And this would be what stores, whether or not they got a given answer right.
-
5:06
So we can go look, we can go okay.
-
5:08
Question number six, which has the fifth index and
-
5:11
answer number six which again, has the fifth index.
-
5:15
What's the question and what's the was the answer they gave true or false?
-
5:20
And that way we can go, oh, you missed question number five.
-
5:23
Here it is again.
-
5:24
Not something we're going to build, but something you could add if you wanted to.
-
5:28
Okay.
-
5:28
So, ask, what are we going to do here?
-
5:31
So, I think we should log the start time, because it might be a neat stat to show.
-
5:36
Again, not something I'm gonna write, but something you might want to add to show
-
5:39
how long it took them to answer each question.
-
5:41
So they can go, oh,
-
5:42
multiplication questions take me longer than addition questions or whatever.
-
5:47
We want to capture the answer.
-
5:49
[NOISE] We want to check the answer.
-
5:54
We want to log the in time.
-
5:57
So how long it took them to answer that question?
-
6:00
And then if the answer is right, send back true.
-
6:05
Otherwise, send back false and
-
6:09
let's send back the elapsed time too.
-
6:13
So that way, we've got back true or false and we've got back, you know, oh,
-
6:18
ten seconds or five milliseconds or, or whatever.
-
6:21
Okay.
-
6:21
And then I think,
-
6:23
the only thing we are missing is a we've talked about having a way to show summary.
-
6:28
So let's have a function that shows the summary print or, sorry, a method print.
-
6:33
Write how many you got right and the total number of questions.
-
6:39
So, you know, that'll be like five out of ten and
-
6:43
then print the total time for the quiz.
-
6:46
So, you know, that'll be like 30 seconds.
-
6:49
Okay.
-
6:50
So, I think that gets us in a good spot and we'll come back and
-
6:54
build the rest next time.
-
6:56
All right.
-
6:58
Well, we have two good classes for asking questions and quite a bit of planning.
-
7:02
Feel free to try and finish the app from here if you want.
-
7:06
I'll show you my solution to it in the next video.
You need to sign up for Treehouse in order to download course files.
Sign up