Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
The Quiz Class7:38 with Kenneth Love
Let's build the Quiz class that'll drive most of our game.
'Kay, so here we are back in quiz.py and let's go ahead and finish this out. 0:00 I think I'm actually going to start down here in the summary. 0:05 I think that will be faster and easier to build first. 0:08 So let's handle this first one, this print how many you got right. 0:12 So let's say, print and 0:16 You got something out of something right. 0:19 And then we'll do a format. 0:26 And we'll put in here self.total_correct and len(self.questions). 0:28 And then we'll close that. 0:36 So, what's with this self.total_correct? 0:38 We're gonna have to remember to make a new function. 0:42 So let's just make a note up here about that. 0:44 Def total_correct. 0:45 Sorry, I keep saying function. 0:48 We're gonna begin new method. 0:50 Return the total number of correct answers. 0:52 Okay. 0:57 So, there's that, and we, I said you got blank out of blank. 0:58 We could do this as like five out of ten, right? 1:01 I just like the, being a bit more verbose there. 1:05 Okay, and then we wanna print how long it took. 1:08 So, let's say It took you blank seconds total. 1:11 And then we're gonna call .format. 1:18 And we're gonna put in self.end_time minus self.start_time .seconds. 1:22 And then we're gonna close that off. 1:30 Okay, so now why do I have parentheses around this? 1:33 This isn't like a function or anything. 1:36 So, the reason is because I want Python to calculate what's in self.end_time 1:38 minus self.start_time. 1:43 I want to, to calculate that and give me back this like temporary 1:45 unnamed invisible variable, which is the time delta, right? 1:48 Because when we subtract one date time from another, we get a time delta. 1:52 And then I wanna get the seconds off of that. 1:56 So this is gonna be, you know, 50, or whatever. 1:58 Okay. 2:02 We have to write this total correct. 2:03 How do we write total correct? 2:04 How do we figure this out? 2:05 Well, there's a couple different ways to do this. 2:06 I would actually do this in a very different way if 2:09 I was doing this just on my own. 2:12 But as far as like, teaching this, and with what I've taught you so far, 2:14 cuz I haven't taught you like functional programming, I so wanna do it. 2:18 Anyway though, since I haven't done that, 2:21 we've gotta do this in a slightly slower way. 2:22 So, here's what we're gonna do. 2:24 We're gonna do total equals 0. 2:25 And for answer in self.answers, all right? 2:28 If answer, then total plus equals 1. 2:34 Return total, 'kay? 2:41 So, what are we doing here? 2:44 Well, so we're kinda doing two things. 2:45 First of all, we are saying that each answer that's in self.answers is 2:46 going to have, it, it's going to be a list or tuple whatever itself. 2:51 And we're saying that the first item is going to be 2:55 either whether they got the question right or whether they got the question wrong. 2:58 So, all I'm gonna put in here is I'm gonna put in True or False. 3:01 So, what we're effectively saying here on 35 is the exact same thing 3:04 as us doing that, if answer equal to True. 3:08 But answer will either be the literal Boolean True or the literal Boolean False. 3:10 So, we don't have to have the equals True. 3:17 We can just say if answer. 3:20 So it's anything truthy. 3:22 This is why we would, we would them, we would not wanna put in, say, 3:23 just an empty string, okay? 3:26 Well, actually that would come back False, so that'd be okay. 3:27 We, we wanna be careful. 3:29 We wanna be sure to actually put in Trues and Falses. 3:30 That's fine. 3:33 That's cool. 3:33 So, all we're missing now is everything else. 3:34 Where do we start? 3:39 Let's actually go back here to the init cuz I think the init's gonna be, and 3:40 [UNKNOWN] to generate ten questions, right? 3:43 That's not that bad. 3:45 Okay. 3:46 So, let's hold on to what our question_types are. 3:47 So, questions_types, we're gonna have two of these. 3:51 We're gonna have Add and we're gonna have Multiply. 3:53 Now, these are the class names that we imported, right? 3:56 Exact same names there. 4:00 So these actually represent the classes. 4:03 So I could now do question_types, 1, and 1, 4:04 and I would get an Add question of 1 plus 1. 4:09 You know, or 1 plus 5, or whatever. 4:13 So, this is now the class. 4:16 And this is when we call the class and pass it arguments. 4:19 It's a little bit weird. 4:22 But it's something you're gonna see more and more often, and 4:24 it's a way that we don't have to have these like throw away extra variables. 4:26 Okay, so we've got our question_types. 4:31 Now we need to generate ten random questions. 4:34 So, we need a for loop. 4:38 I wanna do this ten times. 4:39 So for _ in range(10). 4:41 Oh, what's with using the underscore? 4:44 Why an underscore? 4:47 Well, underscore tells Python 4:48 that we don't really care what the value is that comes out. 4:50 I don't care that it's 0, 1, 2, 5, 8, whatever. 4:53 I just want it to count this number of times, right? 4:57 So, we're basically just saying, throw away the value. 5:00 I don't care. 5:01 Just make sure the loop goes this number of times. 5:02 So, I'm gonna do this ten times. 5:05 Now, this is a place where you might wanna make the app a bit more extensible. 5:07 Maybe when you've instantiate the quiz, you want to say, I want 20 questions, or 5:10 100 questions, or two questions, right? 5:15 Now we want to do something ten times. 5:17 And the thing we wanna do is we want to take self.questions and 5:21 we want to append something to it. 5:25 What do we want to append? 5:27 Well, let's do this. 5:29 We can do question equals question_types. 5:30 Actually here, let's say num1 is random.randint between 1 and 10. 5:34 And num2 equals random.randint between 1 and 10, 'kay? 5:41 Now, this is another place where we might want to make this extensible, 5:45 to where they can say, oh, I want numbers from 5 to 50. 5:51 Now, I have two random numbers, and 5:54 I wanna do a random.choice here cuz I want it to 5:58 grab me a random question type, right? 6:03 I want to be either add or multiply. 6:07 Okay. 6:09 But when I build one of these classes, I have to give it numbers, so 6:10 it gave me back my random number or my random class. 6:13 Now we put in num1 and num2, 'kay? 6:16 And then this appends question. 6:19 So that's what this comment. 6:22 [BLANK_AUDIO] 6:23 Is right here. 6:25 Add these questions in the self.questions, 'kay? 6:26 So, this is kinda crazy looking. 6:30 It's kinda weird, right, calling this random.choice thing, 6:32 and then putting this in? 6:35 It's weird, but it's something you're gonna start to see more and more often. 6:36 This is fairly common, having a, a class that just kind of, 6:41 is in this ephemeral state, and then we're gonna use it. 6:44 We don't actually care about holding onto that class at the time. 6:47 Okay, so we're gonna go through it, and we put pass into each of these functions so 6:51 that they just sit there. 6:54 All right, and now let's test this out. 6:56 We're gonna come down here, and we're gonna do python and from quiz import Quiz. 6:59 'Kay, so we've got a quiz. 7:06 So, let's say quiz1 equals Quiz(). 7:07 And I should be able to do quiz1.answers and get back nothing. 7:12 And I should be able do quiz1s.,. 7:16 quiz1.questions. 7:18 And look at that, I got a whole bunch of questions. 7:19 So, I should be able to look at, say, the first one and see what the text is. 7:23 So, 10 times 5. 7:31 Okay. 7:31 So, when I look at answer here, it should be 50. 7:32 And it is. 7:34 Awesome. 7:35 [BLANK_AUDIO] 7:36
You need to sign up for Treehouse in order to download course files.Sign up