1 00:00:00,605 --> 00:00:05,460 'Kay, so here we are back in quiz.py and let's go ahead and finish this out. 2 00:00:05,460 --> 00:00:08,980 I think I'm actually going to start down here in the summary. 3 00:00:08,980 --> 00:00:12,980 I think that will be faster and easier to build first. 4 00:00:12,980 --> 00:00:16,230 So let's handle this first one, this print how many you got right. 5 00:00:16,230 --> 00:00:19,689 So let's say, print and 6 00:00:19,689 --> 00:00:25,630 You got something out of something right. 7 00:00:26,630 --> 00:00:28,490 And then we'll do a format. 8 00:00:28,490 --> 00:00:35,209 And we'll put in here self.total_correct and len(self.questions). 9 00:00:36,940 --> 00:00:38,300 And then we'll close that. 10 00:00:38,300 --> 00:00:42,310 So, what's with this self.total_correct? 11 00:00:42,310 --> 00:00:44,170 We're gonna have to remember to make a new function. 12 00:00:44,170 --> 00:00:45,816 So let's just make a note up here about that. 13 00:00:45,816 --> 00:00:48,382 Def total_correct. 14 00:00:48,382 --> 00:00:50,340 Sorry, I keep saying function. 15 00:00:50,340 --> 00:00:52,320 We're gonna begin new method. 16 00:00:52,320 --> 00:00:57,250 Return the total number of correct answers. 17 00:00:57,250 --> 00:00:58,510 Okay. 18 00:00:58,510 --> 00:01:01,940 So, there's that, and we, I said you got blank out of blank. 19 00:01:01,940 --> 00:01:05,640 We could do this as like five out of ten, right? 20 00:01:05,640 --> 00:01:08,270 I just like the, being a bit more verbose there. 21 00:01:08,270 --> 00:01:11,226 Okay, and then we wanna print how long it took. 22 00:01:11,226 --> 00:01:18,618 So, let's say It took you blank seconds total. 23 00:01:18,618 --> 00:01:22,116 And then we're gonna call .format. 24 00:01:22,116 --> 00:01:30,062 And we're gonna put in self.end_time minus self.start_time .seconds. 25 00:01:30,062 --> 00:01:33,410 And then we're gonna close that off. 26 00:01:33,410 --> 00:01:36,850 Okay, so now why do I have parentheses around this? 27 00:01:36,850 --> 00:01:38,920 This isn't like a function or anything. 28 00:01:38,920 --> 00:01:43,672 So, the reason is because I want Python to calculate what's in self.end_time 29 00:01:43,672 --> 00:01:45,130 minus self.start_time. 30 00:01:45,130 --> 00:01:48,550 I want to, to calculate that and give me back this like temporary 31 00:01:48,550 --> 00:01:52,500 unnamed invisible variable, which is the time delta, right? 32 00:01:52,500 --> 00:01:56,080 Because when we subtract one date time from another, we get a time delta. 33 00:01:56,080 --> 00:01:58,590 And then I wanna get the seconds off of that. 34 00:01:58,590 --> 00:02:02,440 So this is gonna be, you know, 50, or whatever. 35 00:02:02,440 --> 00:02:03,060 Okay. 36 00:02:03,060 --> 00:02:04,860 We have to write this total correct. 37 00:02:04,860 --> 00:02:05,770 How do we write total correct? 38 00:02:05,770 --> 00:02:06,480 How do we figure this out? 39 00:02:06,480 --> 00:02:09,230 Well, there's a couple different ways to do this. 40 00:02:09,230 --> 00:02:12,040 I would actually do this in a very different way if 41 00:02:12,040 --> 00:02:14,420 I was doing this just on my own. 42 00:02:14,420 --> 00:02:18,040 But as far as like, teaching this, and with what I've taught you so far, 43 00:02:18,040 --> 00:02:21,270 cuz I haven't taught you like functional programming, I so wanna do it. 44 00:02:21,270 --> 00:02:22,850 Anyway though, since I haven't done that, 45 00:02:22,850 --> 00:02:24,920 we've gotta do this in a slightly slower way. 46 00:02:24,920 --> 00:02:25,870 So, here's what we're gonna do. 47 00:02:25,870 --> 00:02:28,010 We're gonna do total equals 0. 48 00:02:28,010 --> 00:02:34,630 And for answer in self.answers, all right? 49 00:02:34,630 --> 00:02:39,375 If answer[0], then total plus equals 1. 50 00:02:41,690 --> 00:02:44,150 Return total, 'kay? 51 00:02:44,150 --> 00:02:45,050 So, what are we doing here? 52 00:02:45,050 --> 00:02:46,810 Well, so we're kinda doing two things. 53 00:02:46,810 --> 00:02:51,550 First of all, we are saying that each answer that's in self.answers is 54 00:02:51,550 --> 00:02:55,690 going to have, it, it's going to be a list or tuple whatever itself. 55 00:02:55,690 --> 00:02:58,590 And we're saying that the first item is going to be 56 00:02:58,590 --> 00:03:01,940 either whether they got the question right or whether they got the question wrong. 57 00:03:01,940 --> 00:03:04,810 So, all I'm gonna put in here is I'm gonna put in True or False. 58 00:03:04,810 --> 00:03:08,730 So, what we're effectively saying here on 35 is the exact same thing 59 00:03:08,730 --> 00:03:10,962 as us doing that, if answer[0] equal to True. 60 00:03:10,962 --> 00:03:17,090 But answer[0] will either be the literal Boolean True or the literal Boolean False. 61 00:03:17,090 --> 00:03:20,130 So, we don't have to have the equals True. 62 00:03:20,130 --> 00:03:22,630 We can just say if answer[0]. 63 00:03:22,630 --> 00:03:23,680 So it's anything truthy. 64 00:03:23,680 --> 00:03:26,460 This is why we would, we would them, we would not wanna put in, say, 65 00:03:26,460 --> 00:03:27,689 just an empty string, okay? 66 00:03:27,689 --> 00:03:29,224 Well, actually that would come back False, so that'd be okay. 67 00:03:29,224 --> 00:03:30,280 We, we wanna be careful. 68 00:03:30,280 --> 00:03:33,130 We wanna be sure to actually put in Trues and Falses. 69 00:03:33,130 --> 00:03:33,780 That's fine. 70 00:03:33,780 --> 00:03:34,710 That's cool. 71 00:03:34,710 --> 00:03:39,160 So, all we're missing now is everything else. 72 00:03:39,160 --> 00:03:40,020 Where do we start? 73 00:03:40,020 --> 00:03:43,239 Let's actually go back here to the init cuz I think the init's gonna be, and 74 00:03:43,239 --> 00:03:45,480 [UNKNOWN] to generate ten questions, right? 75 00:03:45,480 --> 00:03:46,275 That's not that bad. 76 00:03:46,275 --> 00:03:47,070 Okay. 77 00:03:47,070 --> 00:03:51,500 So, let's hold on to what our question_types are. 78 00:03:51,500 --> 00:03:53,280 So, questions_types, we're gonna have two of these. 79 00:03:53,280 --> 00:03:55,050 We're gonna have Add and we're gonna have Multiply. 80 00:03:56,090 --> 00:04:00,980 Now, these are the class names that we imported, right? 81 00:04:00,980 --> 00:04:03,130 Exact same names there. 82 00:04:03,130 --> 00:04:04,508 So these actually represent the classes. 83 00:04:04,508 --> 00:04:09,355 So I could now do question_types[0], 1, and 1, 84 00:04:09,355 --> 00:04:13,180 and I would get an Add question of 1 plus 1. 85 00:04:13,180 --> 00:04:16,700 You know, or 1 plus 5, or whatever. 86 00:04:16,700 --> 00:04:19,320 So, this is now the class. 87 00:04:19,320 --> 00:04:22,630 And this is when we call the class and pass it arguments. 88 00:04:22,630 --> 00:04:24,210 It's a little bit weird. 89 00:04:24,210 --> 00:04:26,770 But it's something you're gonna see more and more often, and 90 00:04:26,770 --> 00:04:31,520 it's a way that we don't have to have these like throw away extra variables. 91 00:04:31,520 --> 00:04:34,160 Okay, so we've got our question_types. 92 00:04:34,160 --> 00:04:38,490 Now we need to generate ten random questions. 93 00:04:38,490 --> 00:04:39,990 So, we need a for loop. 94 00:04:39,990 --> 00:04:41,390 I wanna do this ten times. 95 00:04:41,390 --> 00:04:44,420 So for _ in range(10). 96 00:04:44,420 --> 00:04:47,090 Oh, what's with using the underscore? 97 00:04:47,090 --> 00:04:48,100 Why an underscore? 98 00:04:48,100 --> 00:04:50,610 Well, underscore tells Python 99 00:04:50,610 --> 00:04:53,470 that we don't really care what the value is that comes out. 100 00:04:53,470 --> 00:04:57,070 I don't care that it's 0, 1, 2, 5, 8, whatever. 101 00:04:57,070 --> 00:05:00,190 I just want it to count this number of times, right? 102 00:05:00,190 --> 00:05:01,860 So, we're basically just saying, throw away the value. 103 00:05:01,860 --> 00:05:02,480 I don't care. 104 00:05:02,480 --> 00:05:05,630 Just make sure the loop goes this number of times. 105 00:05:05,630 --> 00:05:07,630 So, I'm gonna do this ten times. 106 00:05:07,630 --> 00:05:10,448 Now, this is a place where you might wanna make the app a bit more extensible. 107 00:05:10,448 --> 00:05:15,400 Maybe when you've instantiate the quiz, you want to say, I want 20 questions, or 108 00:05:15,400 --> 00:05:17,950 100 questions, or two questions, right? 109 00:05:17,950 --> 00:05:21,250 Now we want to do something ten times. 110 00:05:21,250 --> 00:05:25,250 And the thing we wanna do is we want to take self.questions and 111 00:05:25,250 --> 00:05:27,790 we want to append something to it. 112 00:05:27,790 --> 00:05:29,080 What do we want to append? 113 00:05:29,080 --> 00:05:30,530 Well, let's do this. 114 00:05:30,530 --> 00:05:34,350 We can do question equals question_types. 115 00:05:34,350 --> 00:05:40,009 Actually here, let's say num1 is random.randint between 1 and 10. 116 00:05:41,050 --> 00:05:45,820 And num2 equals random.randint between 1 and 10, 'kay? 117 00:05:45,820 --> 00:05:51,020 Now, this is another place where we might want to make this extensible, 118 00:05:51,020 --> 00:05:54,330 to where they can say, oh, I want numbers from 5 to 50. 119 00:05:54,330 --> 00:05:58,240 Now, I have two random numbers, and 120 00:05:58,240 --> 00:06:03,161 I wanna do a random.choice here cuz I want it to 121 00:06:03,161 --> 00:06:07,480 grab me a random question type, right? 122 00:06:07,480 --> 00:06:09,360 I want to be either add or multiply. 123 00:06:09,360 --> 00:06:10,000 Okay. 124 00:06:10,000 --> 00:06:13,170 But when I build one of these classes, I have to give it numbers, so 125 00:06:13,170 --> 00:06:16,750 it gave me back my random number or my random class. 126 00:06:16,750 --> 00:06:19,930 Now we put in num1 and num2, 'kay? 127 00:06:19,930 --> 00:06:22,520 And then this appends question. 128 00:06:22,520 --> 00:06:23,084 So that's what this comment. 129 00:06:23,084 --> 00:06:25,837 [BLANK_AUDIO] 130 00:06:25,837 --> 00:06:26,466 Is right here. 131 00:06:26,466 --> 00:06:30,650 Add these questions in the self.questions, 'kay? 132 00:06:30,650 --> 00:06:32,500 So, this is kinda crazy looking. 133 00:06:32,500 --> 00:06:35,717 It's kinda weird, right, calling this random.choice thing, 134 00:06:35,717 --> 00:06:36,990 and then putting this in? 135 00:06:36,990 --> 00:06:41,570 It's weird, but it's something you're gonna start to see more and more often. 136 00:06:41,570 --> 00:06:44,780 This is fairly common, having a, a class that just kind of, 137 00:06:44,780 --> 00:06:47,590 is in this ephemeral state, and then we're gonna use it. 138 00:06:47,590 --> 00:06:50,000 We don't actually care about holding onto that class at the time. 139 00:06:51,280 --> 00:06:54,770 Okay, so we're gonna go through it, and we put pass into each of these functions so 140 00:06:54,770 --> 00:06:56,628 that they just sit there. 141 00:06:56,628 --> 00:06:59,334 All right, and now let's test this out. 142 00:06:59,334 --> 00:07:06,515 We're gonna come down here, and we're gonna do python and from quiz import Quiz. 143 00:07:06,515 --> 00:07:07,920 'Kay, so we've got a quiz. 144 00:07:07,920 --> 00:07:12,792 So, let's say quiz1 equals Quiz(). 145 00:07:12,792 --> 00:07:16,120 And I should be able to do quiz1.answers and get back nothing. 146 00:07:16,120 --> 00:07:18,275 And I should be able do quiz1s.,. 147 00:07:18,275 --> 00:07:19,800 quiz1.questions. 148 00:07:19,800 --> 00:07:23,460 And look at that, I got a whole bunch of questions. 149 00:07:23,460 --> 00:07:31,040 So, I should be able to look at, say, the first one and see what the text is. 150 00:07:31,040 --> 00:07:31,885 So, 10 times 5. 151 00:07:31,885 --> 00:07:32,630 Okay. 152 00:07:32,630 --> 00:07:34,680 So, when I look at answer here, it should be 50. 153 00:07:34,680 --> 00:07:35,530 And it is. 154 00:07:35,530 --> 00:07:36,384 Awesome. 155 00:07:36,384 --> 00:07:38,569 [BLANK_AUDIO]