1 00:00:03,288 --> 00:00:07,390 [SOUND] So let's use our new found date time abilities to make 2 00:00:07,390 --> 00:00:11,507 an app that will ask us math questions and time our answers. 3 00:00:11,507 --> 00:00:15,028 We're gonna keep it pretty bare bones, but that lets us leave a place or two for 4 00:00:15,028 --> 00:00:15,847 improvements. 5 00:00:15,847 --> 00:00:18,350 If you haven't watched object oriented Python yet, 6 00:00:18,350 --> 00:00:21,528 you'll probably want to before this video, so you won't be lost. 7 00:00:21,528 --> 00:00:22,028 All right. 8 00:00:22,028 --> 00:00:22,548 Ready? 9 00:00:22,548 --> 00:00:25,348 Let's get started. 10 00:00:25,348 --> 00:00:25,969 Okay. So 11 00:00:25,969 --> 00:00:31,397 let's create a few classes that'll make it easier for us to create and ask questions. 12 00:00:31,397 --> 00:00:35,028 Our app is just going to ask addition and multiplication questions. 13 00:00:35,028 --> 00:00:38,332 So, our classes should be pretty simple. 14 00:00:38,332 --> 00:00:41,248 I'm doing this in a file called questions.py. 15 00:00:41,248 --> 00:00:46,788 So let's make a class Question and it's gonna have two attributes. 16 00:00:46,788 --> 00:00:50,468 Answer, which is None and text, which is None. 17 00:00:50,468 --> 00:00:53,128 I'm actually going to leave the class right there. 18 00:00:53,128 --> 00:00:56,348 You would think I'd add more, but I want these to be very simple. 19 00:00:56,348 --> 00:00:58,780 I want them to have a very simple API and 20 00:00:58,780 --> 00:01:01,827 I don't want them to be crazily overextended. 21 00:01:01,827 --> 00:01:04,905 You could definitely add some more to this to make them simpler or 22 00:01:04,905 --> 00:01:06,908 to make them a little smarter if you want. 23 00:01:06,908 --> 00:01:08,508 That's completely up to you. 24 00:01:08,508 --> 00:01:11,492 So now let's make a class called Add and 25 00:01:11,492 --> 00:01:16,928 it's going to extend question and I'm gonna override the init for it. 26 00:01:16,928 --> 00:01:20,682 And it's going to take two numbers and so 27 00:01:20,682 --> 00:01:26,205 we're gonna set self.text equal to this string format and 28 00:01:26,205 --> 00:01:30,748 we're gonna stick in number one and number two. 29 00:01:30,748 --> 00:01:36,748 And self.answer is going to be the actual value of num1 plus num2. 30 00:01:36,748 --> 00:01:37,668 Okay. 31 00:01:37,668 --> 00:01:42,828 So let's do the same thing here for multiply. 32 00:01:42,828 --> 00:01:45,398 In fact, I'm going to copy this and 33 00:01:45,398 --> 00:01:51,445 what we're going to do is we're going to change this, I'm going to put in an x. 34 00:01:51,445 --> 00:01:55,849 You could use the time, the, the star, you could use a unicode time. 35 00:01:55,849 --> 00:01:58,241 You can use the word times, whatever you wanna do. 36 00:01:58,241 --> 00:02:02,042 So the only thing different here is how we calculate stuff. 37 00:02:02,042 --> 00:02:03,818 I think this was pretty quick. 38 00:02:03,818 --> 00:02:07,400 I think this was worth it, but let's test these out. 39 00:02:07,400 --> 00:02:12,719 So let's come down here to our workspace and 40 00:02:12,719 --> 00:02:17,332 let's do from questions import Add. 41 00:02:17,332 --> 00:02:20,442 And we are going to do add1 equals Add and 42 00:02:20,442 --> 00:02:25,031 we are going to pass in numbers, so let's do five and seven. 43 00:02:25,031 --> 00:02:25,951 Okay. 44 00:02:25,951 --> 00:02:29,512 If we look at add1.text, we get 5 plus 7. 45 00:02:29,512 --> 00:02:34,830 And if we look at add1.answer, we get 12, which is right. 46 00:02:34,830 --> 00:02:35,611 That's pretty awesome. 47 00:02:35,611 --> 00:02:37,930 That's, that's what I wanted to do. 48 00:02:37,930 --> 00:02:41,800 So before we move on and wrap up this video, 49 00:02:41,800 --> 00:02:45,571 let's actually go ahead and do our quiz. 50 00:02:45,571 --> 00:02:47,192 We'll do some planning on our quiz. 51 00:02:47,192 --> 00:02:51,310 So New File, we'll do quiz.py. 52 00:02:51,310 --> 00:02:54,891 At this point, well we're gonna have a class. 53 00:02:54,891 --> 00:02:55,631 Right? 54 00:02:55,631 --> 00:02:58,295 And we're gonna want to be able to store and 55 00:02:58,295 --> 00:03:00,890 hold onto all the questions that we have. 56 00:03:00,890 --> 00:03:03,210 So, our questions is gonna be an empty list. 57 00:03:03,210 --> 00:03:03,932 Okay. 58 00:03:03,932 --> 00:03:04,551 So cool. 59 00:03:04,551 --> 00:03:05,191 But oh, wait. 60 00:03:05,191 --> 00:03:07,771 So, if we're gonna use questions, we're gonna have to import them. 61 00:03:07,771 --> 00:03:13,411 So we need to do from questions import Add, Multiply. 62 00:03:13,411 --> 00:03:14,931 So we've got both of those. 63 00:03:14,931 --> 00:03:18,507 We're also gonna wanna track how long it takes to answer these things, so we need 64 00:03:18,507 --> 00:03:22,041 a start time and an end time and we're gonna wanna build these things randomly. 65 00:03:22,041 --> 00:03:27,580 So that means, we're going to need to import datetime and import random. 66 00:03:27,580 --> 00:03:32,041 So you notice that there's a blank line in between datetime random and questions? 67 00:03:32,041 --> 00:03:36,060 That's because my local imports, I wanna have this, this little bit of separation, 68 00:03:36,060 --> 00:03:39,045 just so I can see, like, okay, these are top level right here. 69 00:03:39,045 --> 00:03:43,052 And these are my own custom ones right here and so on. 70 00:03:43,052 --> 00:03:46,226 By top-level, I mean like built into the standard library. 71 00:03:46,226 --> 00:03:48,186 You don't have to do this, but it's a good habit. 72 00:03:48,186 --> 00:03:48,966 All right. 73 00:03:48,966 --> 00:03:55,446 So when we initialize our quiz, we're gonna want to generate some question. 74 00:03:55,446 --> 00:03:56,486 Right? 75 00:03:56,486 --> 00:04:00,305 So let's, I don't know, we could probably do that in def init. 76 00:04:00,305 --> 00:04:05,284 And so let's just say, generate 10 random 77 00:04:05,284 --> 00:04:09,747 questions with numbers from 1 to 10. 78 00:04:09,747 --> 00:04:15,006 And then we'll say, add these questions into self.questions. 79 00:04:15,006 --> 00:04:15,666 Okay. 80 00:04:15,666 --> 00:04:16,446 Pretty simple. 81 00:04:16,446 --> 00:04:21,026 Let's add a new method here called take_quiz. 82 00:04:21,026 --> 00:04:24,866 And I think this is gonna be the method that actually, like asks the questions. 83 00:04:24,866 --> 00:04:26,205 So, let's see. 84 00:04:26,205 --> 00:04:32,366 Log the start time and ask all of the questions. 85 00:04:32,366 --> 00:04:37,534 And log if they got the question right and 86 00:04:37,534 --> 00:04:42,706 log the end time and then show a summary. 87 00:04:42,706 --> 00:04:43,386 Okay. 88 00:04:43,386 --> 00:04:48,251 This makes me think that since we've got this, this ask all the questions 89 00:04:48,251 --> 00:04:52,980 it makes me think that maybe we should have a method that asks the questions. 90 00:04:52,980 --> 00:04:56,880 So let's call it maybe ask and it'll have a question. 91 00:04:56,880 --> 00:04:58,047 But you know what? Before we do that, 92 00:04:58,047 --> 00:05:00,341 we'll put a pass in there, just to let these things pass. 93 00:05:00,341 --> 00:05:03,521 Let's go up here and add an answers list. 94 00:05:03,521 --> 00:05:06,980 And this would be what stores, whether or not they got a given answer right. 95 00:05:06,980 --> 00:05:08,181 So we can go look, we can go okay. 96 00:05:08,181 --> 00:05:11,753 Question number six, which has the fifth index and 97 00:05:11,753 --> 00:05:15,480 answer number six which again, has the fifth index. 98 00:05:15,480 --> 00:05:20,080 What's the question and what's the was the answer they gave true or false? 99 00:05:20,080 --> 00:05:23,241 And that way we can go, oh, you missed question number five. 100 00:05:23,241 --> 00:05:24,561 Here it is again. 101 00:05:24,561 --> 00:05:28,221 Not something we're going to build, but something you could add if you wanted to. 102 00:05:28,221 --> 00:05:28,821 Okay. 103 00:05:28,821 --> 00:05:31,040 So, ask, what are we going to do here? 104 00:05:31,040 --> 00:05:36,541 So, I think we should log the start time, because it might be a neat stat to show. 105 00:05:36,541 --> 00:05:39,884 Again, not something I'm gonna write, but something you might want to add to show 106 00:05:39,884 --> 00:05:41,761 how long it took them to answer each question. 107 00:05:41,761 --> 00:05:42,900 So they can go, oh, 108 00:05:42,900 --> 00:05:47,681 multiplication questions take me longer than addition questions or whatever. 109 00:05:47,681 --> 00:05:49,121 We want to capture the answer. 110 00:05:49,121 --> 00:05:54,441 [NOISE] We want to check the answer. 111 00:05:54,441 --> 00:05:57,801 We want to log the end time. 112 00:05:57,801 --> 00:06:00,921 So how long it took them to answer that question? 113 00:06:00,921 --> 00:06:05,420 And then if the answer is right, send back true. 114 00:06:05,420 --> 00:06:09,563 Otherwise, send back false and 115 00:06:09,563 --> 00:06:13,981 let's send back the elapsed time too. 116 00:06:13,981 --> 00:06:18,220 So that way, we've got back true or false and we've got back, you know, oh, 117 00:06:18,220 --> 00:06:21,061 ten seconds or five milliseconds or, or whatever. 118 00:06:21,061 --> 00:06:21,840 Okay. 119 00:06:21,840 --> 00:06:23,001 And then I think, 120 00:06:23,001 --> 00:06:28,661 the only thing we are missing is a we've talked about having a way to show summary. 121 00:06:28,661 --> 00:06:33,643 So let's have a function that shows the summary print or, sorry, a method print. 122 00:06:33,643 --> 00:06:39,951 Write how many you got right and the total number of questions. 123 00:06:39,951 --> 00:06:43,549 So, you know, that'll be like five out of ten and 124 00:06:43,549 --> 00:06:46,391 then print the total time for the quiz. 125 00:06:46,391 --> 00:06:49,292 So, you know, that'll be like 30 seconds. 126 00:06:49,292 --> 00:06:50,691 Okay. 127 00:06:50,691 --> 00:06:54,824 So, I think that gets us in a good spot and we'll come back and 128 00:06:54,824 --> 00:06:56,490 build the rest next time. 129 00:06:56,490 --> 00:06:58,471 All right. 130 00:06:58,471 --> 00:07:02,890 Well, we have two good classes for asking questions and quite a bit of planning. 131 00:07:02,890 --> 00:07:06,051 Feel free to try and finish the app from here if you want. 132 00:07:06,051 --> 00:07:08,210 I'll show you my solution to it in the next video.