Branch and Loop9:26 with Craig Dennis
We need to branch and loop to make this project loop. Use recall to make things tick!
Okay, so looking at these, why don't we go ahead and grab this one first? 0:00 So as the user, I should be able to confirm my order so 0:05 that I do not accidentally purchase more tickets than intended. 0:08 And I wanna do that one because I know that that's right after what we just did. 0:12 We just showed them the price, so let's prompt and 0:16 see If they want to buy them, let's do it. 0:19 So put that In Progress. 0:22 Let's flip over here. 0:25 So we just showed the total due. 0:26 So let's come in here and let's ask if they want to continue, all right? 0:28 So we'll say Prompt user if they want to proceed. 0:33 And typically the way that you do that in these console applications is you 0:41 ask them for a Y or an N, for a yes or no. 0:45 So we can do that, and then if they answer that they do wanna proceed, 0:48 so If they want to proceed, we got a couple of things, right? 0:54 So we need to branch out on that decision. 1:00 So let's, how are we gonna mark this? 1:02 Let's do it this way. 1:04 I'll do this on separate lines. 1:05 So if they want to proceed, we're going to print out to the screen. 1:07 Let's just print out SOLD!, so that we can confirm the purchase cuz 1:12 remember we're not gonna process the credit card. 1:16 So we'll say to confirm purchase. 1:18 And because they did purchase it, we need to decrement, or 1:22 reduce by the number of tickets that they actually bought, right. 1:26 So let's see. 1:30 And then decrement, or reduce by, 1:32 the tickets remaining, 1:36 by the number of tickets purchased. 1:42 Cool, so that's if it did work. 1:48 So, lets do that, we'll give you separate line there. 1:50 And then, of course, we'll have our Otherwise. 1:53 So, they wanna keep it friendly, right? 1:59 So, let's just go ahead and otherwise let's thank them by name, 2:01 that sounds good. 2:06 All right, so 2:08 it sounds like some branching logic based on their decision of Y or N, you got it? 2:09 Now go ahead and take these line by line, you got this. 2:15 Check the teacher's notes and use the forum if you get stuck. 2:18 Ready? 2:21 Pause me. 2:22 Okay, all set? 2:23 So here's what I did, I prompted again and I was using input and 2:25 I stored that in a variable called should proceed pretty clear right? 2:28 input ("Do you want to proceed? 2:33 And I gave them a Y/N and just wrote it there. 2:38 Give a couple of spaces. 2:41 And so that will come up and they'll say yes or no. 2:45 And so if they want to proceed, so if should_proceed. 2:48 Now something I did here. 2:53 As I made, I'm checking the lowercase version of should proceed. 2:54 Just in case they entered a lowercase y or a capital y. 2:58 Either one, if either one of those is y, right, lower. 3:00 So, if y is equal to y, we're gonna open up that body and 3:06 we're gonna print that to the screen SOLD!. 3:11 And it looks like we are indented too far here, aren't we? 3:16 Let's go ahead and bring this back. 3:20 Staring to feel a little awkward there. 3:23 There we go. So we're gonna print sold, 3:27 and then we're gonna decrement the tickets remaining. 3:29 And you know what I'm gonna do? 3:33 I'm gonna add a reminder here for us. 3:34 I'm gonna put in a TODO. 3:36 Spanish speakers sometimes think that that means all. 3:38 I've had worked with a few native Spanish speakers and to do they think it's 3:41 like to-do, like all, it means to do like we're gonna do this later, 3:46 so we're gonna gather credit card information and process it. 3:51 Awesome. 3:57 And that's already on the Trello board, we won't commit to completing it just yet, 3:58 but I'm gonna leave this here so next time we come into the code we know it's there. 4:02 And now I'm gonna use in place subtraction on tickets remaining, 4:06 just like we've done in place addition, you can also do it with subtraction. 4:09 So tickets_remaining -= num_tickets, right. 4:13 So, that's shorthand for tickets remaining = tickets remaining- num_tickets. 4:19 Just using that -= for in place subtraction. 4:24 And so this Otherwise, this is an else clause here, right? 4:27 So else, we're gonna print, otherwise were gonna thank them by name, 4:31 and we'll print, well, Thank you anyways. 4:39 Placeholder, and we'll format that. 4:44 Pass on that name, close that up. 4:48 Okay, I'm gonna save this, and give it a run, 4:51 let's see what mine looks like right now. 4:54 How do you do? 4:56 There are a hundred tickets remaining. 5:00 What is your name? 5:01 My name is Craig, I would like to have 3 tickets. 5:02 The total due is $30, do you wanna proceed? 5:05 No way. 5:09 So it's not Y, right? 5:10 So it says thank you anyway. 5:11 So let's do the other one. 5:12 My name is Craig, Crag, sure, and 5:13 I wanna buy four tickets, and do you wanna proceed? 5:18 Yes as a matter of fact I do but I lower case y, want to proceed. 5:22 Sold. 5:25 So I don't actually know if that decrementing the tickets remaining 5:26 actually worked but I think maybe another ticket will get us there. 5:30 So, let's flip back to the Trello board real quick. 5:33 Let's look over there. 5:38 I think we're done with this confirmation one. 5:39 Nice job. 5:41 Okay, so as a user, 5:43 I should not be offered tickets if there aren't any available. 5:44 Let's move that over to In Progress. 5:48 Awesome, and let's flip back. 5:50 So, I think we can tackle this one but 5:53 we're going to need to assume that there's some sort of continuum, right? 5:55 This code will continue to run until there aren't any tickets left. 5:59 So, let's see, we want all of this code, right? 6:03 We want all of this code that we wrote, like from hereon down. 6:08 We want that to go, right? 6:12 Makes sense, so we want to run this code 6:13 continuously until we run out of tickets. 6:18 Makes sense, so this just keeps on prompting, keeps on saying that stuff. 6:24 And then at the very end we need to let them know when they sell out, so 6:27 let's just scroll all the way down here. 6:31 Look at this, we're at 38 lines right now. 6:34 So, we are going to, here we are gonna notify 6:36 the user that the tickets are sold out, of course. 6:40 After they are sold out, after the continuum has happened. 6:46 So one thing before I let you go, I wanna show you this cuz it seems fair. 6:51 If you highlight this you can go ahead and you can do an Edit, and you can Indent. 6:54 And you'll see here on my Mac, it's command and then right bracket. 6:59 So if you highlight some code, you can do a command bracket or left 7:03 bracket to go in and out to indent, that helps when you wanna move a block over. 7:07 So you've got this, right? 7:13 You wanna run this code while there are still some tickets left. 7:15 And then at the very bottom, 7:19 you want to print Sold Out when there aren't tickets available anymore. 7:21 You got this. 7:25 Ready? 7:26 Pause me. 7:26 Okay, so, here's how I did it. 7:29 So while there are tickets remaining. 7:32 And then I went all the way down to the bottom. 7:38 I held down a Shift and went all the way down to the bottom here. 7:40 And then I went ahead and indented that. 7:46 And down here we're just gonna say print("Sorry 7:51 the tickets are all sold out!!! 7:56 I'm gonna make an emoji sad face, because that seems aprops. 8:00 Now, note, I used the truthiness of tickets remaining. 8:04 When this gets down to zero, it will be false. 8:08 Remember, any number other thanzero is true. 8:11 You know what though, since I just had to explain that to you, 8:14 maybe I should be more explicit. 8:18 So let's do that, tickets remaining are greater than or equal to one, 8:20 we will run this code and then we will notify them when they're sold out. 8:26 Okay, let's see how we did. 8:32 We'll say python masterticket. 8:37 What is your name? 8:40 My name is John and I would like to have 98 tickets please. 8:41 Total due is 980, that seems reasonable, let's do it. 8:45 There's 2 tickets left. 8:49 So now Terry comes in. 8:51 He buys 2. 8:53 Total due is 20, Terry wants it. 8:54 Sold, and now the tickets are all sold out. 8:58 Boom. 9:00 Nice job. 9:02 No more offering of tickets. 9:03 All right, so let's move this over. 9:05 This is done-zo. 9:06 Feels so good doesn't it? 9:08 Making something done. 9:09 So, how'd you do? 9:10 If you had any problems, remember to ask in the community, 9:12 if anything at all isn't clear, we'll get it sorted out. 9:15 Okay, we'll tackle those user friendly errors next. 9:18 It's our last ticket, I'm gonna move it to In Progress. 9:22
You need to sign up for Treehouse in order to download course files.Sign up