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