Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Handle Exceptions9:05 with Craig Dennis
Let's catch a couple of exceptions and raise one of our own.
I'm going to clean up these comments, they're getting a little busy, 0:00 these instructional ones. 0:03 So we'll get rid of this, so I'm using again, 0:04 Command + Shift + D to delete some lines here. 0:07 Let's bring this up. 0:10 Don't need that anymore, don't need that or that or those. 0:12 Let's get rid of that, 0:16 I'm going to leave that to-do because we definitely need to do that later. 0:17 Get rid of this. 0:21 Otherwise, and thanks. 0:23 Sold tickets. 0:25 This line, there we go. 0:27 And it's actually 20 lines of code, awesome. 0:28 That feels better. 0:32 I'm gonna save it, and now, when we get this cleaned up, 0:32 should we walk this real quick with those missing? 0:37 Remember, that we are on the, as a user I should have errors reported in 0:39 a user-friendly manner, which is kind of always the case. 0:43 But we really wanted to make sure that we handled this cuz they saw one of 0:46 the tracebacks that I left earlier. 0:49 So all right, here we go. 0:51 First off. 0:53 So here, we have a constant and it's in all upper case, it won't change while 0:53 the program's running and if the group wants to change this in the future, 0:58 it's a very easy place to change it, constants. 1:02 So these are how many tickets are currently available, now note, 1:06 it's not all uppercase. 1:11 So we can assume that this is going to change. 1:13 And then we have, while there is one or more tickets remaining. 1:16 Okay, we're going to keep on living through all this code. 1:21 So we're going to show how many are left. 1:25 Then we're going to grab their info, what's your name, how many tickets, 1:28 we're going to coerce that. 1:31 Because input always returns a string, but 1:33 we need to use num_tickets for a math purpose. 1:36 So we definitely need an integer. 1:39 Now, it's an integer because you can't buy fractions of a ticket. 1:42 I'd like to buy one and a half tickets please, that doesn't work. 1:45 So if you remember, this seems a little bit dangerous, doesn't it? 1:48 What if the user gave us something that we couldn't coerce? 1:52 Let's keep this in mind for a possible problem. 1:56 Okay, and then we calculate the price. 1:59 And then we show them the total. 2:02 And we ask them, yes or no, do you want to proceed? 2:04 And because we aren't sure if they are going to use an uppercase Y or 2:07 lowercase in responding, we lowercase it. 2:10 And we check. 2:13 We left a nice to-do message here that the SOLD is not really taking any credit 2:15 cards, and then we subtract the number of tickets from the tickets remaining. 2:20 What would happen if the user entered 1,000 tickets? 2:26 Whoa, that'd be negative 900 tickets remaining. 2:30 The while loop up here, this would stop, right? 2:34 Cuz it'd be negative 900, that would stop. 2:36 It's not greater than or equal to one, but 2:39 still, the 1,000 ticket sale would go through. 2:41 We've got to catch that, right? 2:44 So and then finally, anything other than y, 2:46 we're going to say Thank you anyways, and we're going to be friendly about that. 2:49 And then we're going to let people know that those tickets are sold out, 2:53 because the loop's over. 2:56 So once that loop's over, they know. 2:57 Did that feel pretty good being able to read most of that? 3:00 That's pretty cool, right? 3:02 Pretty amazing. 3:03 All right, now, let's take care of these errors. 3:05 Let's do one at a time. 3:08 So the first problem that we have is here, one around coercion, right? 3:10 So remember, the user can add anything here. 3:15 So let's run it to see how bad the errors actually are. 3:17 I want to clear this. 3:24 Let's run python masterticket, and our name is Gallahad. 3:26 How many tickets would you like? 3:33 Blue. 3:35 Blah, what a gross error. 3:36 And our program ended. 3:39 We can't have that happening. 3:41 We won't be able to sell anything more if we drop out like that. 3:42 So it throws a ValueError. 3:45 I'm gonna go ahead, I'm gonna copy that. 3:49 And right above here, right above int( num_tickets), let's say expect, 3:55 I'm gonna paste a ValueError to happen. 4:00 And handle it appropriately. 4:04 Let me give you a hint, remember to test it out. 4:12 Pause me, and give it a go. 4:16 Are you ready? 4:17 All right, ready? 4:19 This is how I went about fixing it. 4:21 So I definitely needed a trial block. 4:22 So I did the block here and we wanna be careful about the num_tickets. 4:25 And I want to accept, we know that that throws a ValueError. 4:33 And what we wanna say is print, 4:38 no, we ran into an issue, 4:44 please try again. 4:49 Now, that feels good, doesn't it? 4:52 But I'm gonna go ahead and run this. 4:54 My name is Gallahad and I would like. 4:57 How many tickets would you like blue. 5:01 Total due is blue blue blue blue blue. 5:04 What? 5:06 How is blue blue blue blue blue. 5:07 Yes. 5:11 Remember, this is where the error happened and the assignment never happened. 5:13 So num_tickets here is the string blue. 5:17 And because you can multiply strings, 5:21 right, repeat the same thing a number of times. 5:24 This repeated however marks the ticket price was which is ten, so 5:26 those are ten blues right there. 5:29 So in order to fix that, I need to put this code in an else. 5:33 Cuz we only want this code to run if we know how many there are. 5:40 So let's do this, and I'm going to indent it with command right bracket. 5:43 Then let's go ahead, and if you want to ever stop out of a program while 5:51 it's waiting at a prompt, you can do a control C and it will send this interrupt. 5:55 So let's run that one more time, make sure it's working. 6:02 My name is Gallahad, blue. 6:05 It says, no, we ran into an issue, and then it just keeps on looping. 6:09 There's 100 tickets remaining, awesome. 6:11 Okay, now we have another error, too, don't we? 6:13 We don't want to allow for that 1,000 ticket sale, right? 6:15 We need to block that. 6:18 So why don't we do that in the try portion here? 6:20 So if we come up here into our try, 6:23 we wanna make sure that we have a valid number of tickets to request, right? 6:27 So we will raise a ValueError if the request is for 6:31 more tickets than are available. 6:39 And then I wanna make sure that that's a user friendly error that we raise. 6:44 So what I want to do next is I wanna include in the output here, 6:48 we want to include the error text in the output. 6:55 All right. 7:01 Remember, you can lean on the community. 7:02 And also, you can rewatch the previous video where we did something similar, 7:04 check the Teacher's Nets. 7:07 You got this. 7:09 Are you ready? 7:10 Pause me. 7:10 Ready? 7:12 So here's what I did. 7:13 So I used an if statement up here. 7:14 So I said, if the number of tickets, that's what they asked for is greater 7:17 than the tickets_remaining, you can't do that, you can't buy those tickets. 7:21 So, what we did was we raised a ValueError. 7:27 And I'm gonna go ahead and be very specific, there are only, 7:33 use a placeholder here, tickets remaining. 7:37 Nice try. 7:43 So then we'll do format and we'll say tickets remaining, 7:45 and then we'll do a closing. 7:53 Okay, and then if I want to use that exception, 7:55 you have to use the as keyword, did you remember that? 7:58 If not, don't fret, we only did that one time in this course. 8:02 The more you practice, the more you will be able to recall it. 8:05 So, you have to use the as keyword, so as err. 8:08 So, no, we ran into an issue, I'm gonna go ahead and drop our message there, 8:12 then I'll, was that a period, space, please try again. 8:18 And then we will format that with our error, format, err. 8:22 There we go. 8:27 Okay, I'm going to break out of this, 8:31 I'm going to clear this and run it one more time. 8:33 What is your name? 8:38 It is Sir Robin. 8:39 I would like 10,000, no we've ran into an issue, there only 100 tickets remaining, 8:41 please try again, there are 100 tickets remaining. 8:46 Awesome, so I think we can move this final ticket over. 8:50 How good does that feel? 8:57 I am gonna go run a demo for Money Python and see what they think, 8:59 they're gonna love it, I know they're gonna love it 9:03
You need to sign up for Treehouse in order to download course files.Sign up