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