Python for Absolute Beginners Transcripts
Chapter: Error handling
Lecture: Catching invalid numerals

Login or purchase this course to watch this video and the rest of the course contents.
0:00 We've identified the problem
0:01 here's where our program is crashing
0:04 when we type in the word seven
0:06 instead of the number 7.
0:07 So what we can do is
0:08 we can actually use Python's error handling
0:11 exception handling behavior.
0:13 So the way that Python works, is
0:15 yes we get back inputs
0:17 and we can check certain things, like
0:19 we can check that they, you know
0:20 we could say, if text is None or not text.strip
0:26 we get return, nothing or I can say
0:28 Oh, that's a problem, we could print out
0:30 you have to enter something, okay Print.
0:38 Alright, if we do this, it'll get us somewhere
0:40 it'll get us a little farther, we come down here
0:41 and my name is Michael, I want to enter that.
0:44 It says, you must enter a response.
0:46 great English. Try again.
0:48 You must enter a response.
0:51 Uh, there we go. Let's put in seven.
0:53 Oh, seven, nope. Didn't work. Did not work.
0:57 There's a small problem
0:58 I think was just the stuff I copied over.
1:00 But, the problem is, yeah we're checking this here
1:03 but, remember, if I type in the word seven
1:05 It's going to pass this. And then it's going to try to convert
1:08 it to an integer, and it's going to crash.
1:09 So the point is, that Python, it's understanding
1:14 is that it's going to raise, what is called
1:17 raise an exception. And instead of returning a value
1:19 it's going to just say
1:20 We're done executing, now we're in error state
1:24 we're in error mode, now we're going to just work
1:25 our way through the program
1:27 until we find some error handling code.
1:30 So what we want to do, is we want to go in here and add
1:32 some of that error handing code into this section
1:35 for the moment.
1:36 So here's how it works, what you do in Python
1:39 and many programming languages actually
1:40 C#, C++, Java and so on, is you say
1:43 we're about to enter something that might not work.
1:46 It might not work at this level
1:47 or it might not work deeper down
1:49 in a function that's called by a function
1:51 that's called by a function
1:52 down there, something might go wrong
1:53 so instead of being certain its going to work
1:55 what you're going to do is you're going to take an attempt at it.
1:57 You're going to try.
2:00 We create a try block when we put all the code in here
2:03 that we hope is going to work, right?
2:05 All of this stuff, we're expecting to work
2:07 but if somewhere along the way, in line on 39
2:10 there's an error, we can say, there's
2:12 except, we're going to try this, except for
2:14 there's some kind of problem, return None.
2:16 And then, let's print out, there was some weird error.
2:22 Now, lets also make this colored, so it's easy to see.
2:25 Make it red, here we go.
2:30 Notice Pycharm has given us a little bit of a warning
2:32 I'll talk about that in a second
2:33 but let's see where we are with this.
2:35 Alright, my name is Michael, I'm here to throw a five.
2:38 What we're going to do if I press five
2:40 is we're going to come along and just go through the try spot.
2:44 We're going to make our way down and eventually
2:46 we're going to either return none
2:48 or we're going to return something.
2:49 We're only going to end up into this section if there's a crash
2:53 okay, like we saw with that invalid literal.
2:56 So we're just say five, that should be fine
2:58 make that a little bit bigger
3:00 not sure why there was some kind of lag
3:01 but I rolled five, Nat picked sponge
3:04 computer picked sponge let's do seven
3:06 then number, then numeral, seven.
3:08 That worked, I took the round, yes.
3:11 Let's keep going, seven. That's working for me.
3:12 Nope, a tie again. But what if I say seven.
3:15 Remember this crashed, but now what's going to happen
3:17 is it's going to come down till it gets to here
3:22 it's going to make it's way down to here
3:23 and then it hits this, it goes oh, error.
3:24 There's an error, stop what you're doing
3:26 jump immediately to the next exception handler
3:29 the next except block, and deal with that.
3:33 Okay, so let's try that.
3:34 I'm going to come down here and I'm going to type seven.
3:35 Instead of the big crash we saw
3:37 we get oh, there was some weird error. Try again.
3:39 Remember, we don't now what's going on, we just said
3:42 we ended up in this error case.
3:43 There could be many reasons why.
3:45 So, yeah that's not great. Let's try hitting this button.
3:49 Oh, there was some weird error again, and now
3:51 oddly it has this skull, like uh, that really didn't work.
3:54 We tried to stop it from language
3:56 actually it's raised in an error
3:58 which got caught right here, you have to force kill it
4:00 if you really want it to go away, okay.
4:03 So, we need to be more, gentle, more specific about this
4:07 so we can come down here and say
4:08 let's just for a second, just tell it to throw
4:11 the error again, just so we can see it really quick.
4:15 Notice right here, this line
4:17 the error is ValueError, bunch of details
4:20 forget the details, what we care about is ValueError.
4:23 We go here, we can say we're going to be more specific
4:26 we're going to except ValueError as a variable if we want.
4:31 We can come down here and say
4:36 could not convert to integer
4:38 and we could just say that
4:39 we know because it's a ValueError
4:41 that this happened most likely.
4:43 But we can also give them some more details and we can say
4:46 V-E- here if we want and we also want to return None
4:50 that's our single to the loop that
4:52 hey, that get input didn't work, try again.
4:55 Michael, and let's just put junk
4:57 were going to go to our error handling
4:59 and it said, could not convert to integer
5:01 invalid literal for int with base ten
5:04 that junk I typed and it says try again.
5:06 Let me type seven
5:08 No, how about seven like that, works like a champ.
5:11 So the normal path goes through the try
5:13 and then just leaves, but if there's an error
5:15 it jumps to this except block.
5:17 Now, we've got a much more friendly game.
5:20 Instead of just going, boom well, I guess the games over
5:22 'cause you didn't type in one to seven exactly right
5:25 it just says, no, no wrong number
5:26 why don't we try again?
5:29 The way that we did that, is we added a try block here
5:32 and an except block that caught the error
5:35 we do not have to have this as V-E
5:37 if you just want to say, this could not convert to an integer
5:41 you don't care about the details
5:42 you don't need to say the variables
5:43 but if you want the details of what went wrong
5:46 then you need to say the variable.
5:47 You can also say like, this
5:49 but you saw that Pycharm didn't like it
5:51 and saying like, you're going to catch everything
5:53 even that when I tried to exit the program
5:56 it didn't work, so, it's not, not great.
5:59 So, let's try to be specific and say
6:00 I'm looking through this error, here.