Bummer! This is just a preview. You need to be signed in with a Treehouse account to view the entire video.
Exceptions4:50 with Kenneth Love
Anticipating and catching exceptions is a very important part of Python development.
Python tends to fail often and loudly when it encounters a problem. This is a good thing as it encourages you to write more defensive code instead of just trusting that things will always work correctly.
Python also tries to be very readable, so you get to use keywords like
If you try
int(.75) in Workspaces' console, you'll find that it works fine and gives you
0. But that's because
.75 is already a number in Python. If you try it with
int('.75'), it'll fail because it can't make an integer from the string because the string doesn't contain an integer. Since this is always possible, we use
try around it to save ourselves if we get bad input.
try - A keyword that marks a block of code to be tried that will watch for exceptions to happen.
except - A keyword that catches exceptions and runs some other code when one happens.
Exception - A catchable/predictable mistake in code, like trying to turn the string
'a' into a number, which would throw a
not - A keyword that negates whatever follows it.
in - A keyword that checks for membership.
round() - A function that rounds a float to the nearest integer.
Index - The position of something inside of something else. This will be a number and counting starts at zero.
In our last video, we printed out a string multiple times.
Let's make something a little bit more complicated that time.
Let's take a string and a number and if it's an
int, get the letter that's at that position in the string.
But if they gave us a float, let's use it as
a percentage and get the letter that distance through the string.
We'll do this in a script named percentletter.py.
So we'll make our file, and we're gonna call this string er, percent_letter.py.
And first of all, we need to get two inputs.
We've been doing this a lot lately, so you should be used to it by now.
User string equals input
what's your word?
And usernum input what's your number?
Okay, so let's try and convert this number, which is now
a string because of the input, in to an actual number.
This might blow up so we should put this into a
container, you know think like a bomb squad containing a bomb.
And so we should use the try keyword, and try, just
like if and everything that ends with a colon is a block, so we tab in one.
So we'll say our num equals int of user num.
So whatever number they gave us try to turn that into an int.
Except if that blows up, then we'll say our
num is equal to the float of their num.
We're being a little naive.
We're assuming that they're going to give us a float or a number.
Something that converts cleanly.
We also need to use an if, so that we know
which direction to take our code once we have a number.
So let's do ifnot.inusernum.
So if there is not a dot in the user num, which is a string that they gave us.
Then we want to do something.
If there's not a dot, then that means they gave us and int.
And like we said, we want to print out the number at that spot.
If there is a dot there, though.
that means they gave us a float.
And that means that we need to find our way to that percent of the string.
So make a new variable here named ratio.
And it'll contain the rounded version of the length
of the user string times the number that we got.
So whatever the length of the string is times the number that we got
which has a decimal in it, and then we round that to the nearest integer.
And we want to print out whatever's that user string
at that point.
Okay, let's try this out.
Let's come down to the bottom and do python percent letter.
What's your word?
Okay, I'm going to use the word Magic.
My number is 1.
Which that gives me the a.
Okay, let's try it again.
I'll use Magic as well.
And I want the number that is 75% of the way through.
And that's the c.
We have rounding issues, so it moves up or down.
Let's try it one more time.
And I want the number that's right in the middle.
And that's the g.
Wow, quite a bit of new stuff in there, huh?
Let's look at the pieces individually.
These two keywords tell us to let Python try
something, but if anything goes wrong, to do something else.
We had it try to turn the number into an
integer, but if that failed, to make it a float instead.
The in keyword tells Python to check and see if something is in something else.
Like checking to see if the prize is still in your box of cereal.
We check to see if there is a decimal
point in the number that our user passed in.
The len function is the length of something.
In our case, it got the length of the string our user passed in.
We don't have separate length methods on each type
of data like a lot of other languages do.
The round function rounds a float to the nearest whole number.
So 2.4 becomes 2 and 3.9 becomes 4.
Just like your math drills in third grade.
And finally, what's up with the weird square braces after our string variable?
We use these to get things from a string, or a list,
which we'll talk about in the next stage, based on an index number.
Indexes start at 0 so count accordingly.
You need to sign up for Treehouse in order to download course files.Sign up