2.3 Debug errors - Video Tutorials & Practice Problems
Video duration:
12m
Play a video:
Video transcript
<v ->Okay.</v> So, we've looked a little bit about types and variables and now, um, and I've kind of pushed those errors aside, until this lesson. So, we're gonna look at errors, um, and first we're gonna try to solve problem number three, which is doing a temperature converter. So, me being Canadian, I used to live in San Francisco for a little while and people would always talk about temperature in Fahrenheit and I'd be like "I have no idea what you're talking about". So, we're gonna write a little program, to convert Fahrenheit temperatures into Celsius. So I'm gonna open up "Problem 3, Temperature Converter", and the documentation here says "Given a temperature in Fahrenheit, return the temperature in Celsius". So first we want to ask for temperature in Fahrenheit. And, we'll do that by using the input function, to get user input. And, I'm just gonna say 'Temp in F'. And we'll run that. I'm gonna run my code frequently to see, so that I can catch mistakes, hopefully as they happen. So "temp in F 23". Okay, and nothing outputs, because we don't have any print statements. Um, and then we want to save this input as a variable. So, I'm gonna call it 'Temp_F=', so Fahrenheit temperature. And then, print what Temp F is. Okay. So it's printing out as 23. It also has, a little space in front of it and that's because I actually typed in a space before here. And we'll see if that actually screws anything up. So, that's good, we're saving it to a variable. And now we want to calculate this temperature in Celsius, so, I'm gonna look back at this equation here, and it says the temperature in Celsius is equal to the temperature in Fahrenheit, minus 32 and then times five, divided by nine. Temperature in Celsius, is equal to the temperature in Fahrenheit, minus 32 and then that is all going to be in parenthesis because then afterwards, we want to multiply it by five, divided by nine. And, then we're gonna print this out. We need to tell the user and output something. We'll say, "Temp in C" and then "+ temp C". So now let's run this, "temp in F 23". Okay. So it's giving us an error and the trace back is a little more useful when we're running another file, because it tells us which file, we found it in. What line and what that line was. So here, line eight. And it's saying "Type error unsupported operand type for minus str and int. And, you can see that Pycharm was already warning us, that this, it expected and integer but got a string instead because we're trying to subtract something, and maybe we can't subtract integers from strings. So if we wanted to check that, I'll try "Hello - 1" And I get the same error. Um, I'm curious. Can I subtract a string from a string? No, I can also not subtract strings at all. So I'll go back into my run, which is where this code was running. And, so how do I, figure this out? Well one thing I can do, if I'm totally stuck, I can actually, um, and I have no idea what the air message is, I can search this on the internet. So, if I say "Python"... here, and then, I right away get this one result and it's from stackoverflow.com, and this is a very useful website for question and answer on programming related things. So, if someone is new to Python and they are getting an error, and then people are giving responses. So one says "for future reference, Python is strongly typed, unlike other dynamic languages, it will not auto-magically cast objects from one type to the other." Like a str to and int, so you'd have to do this yourself. So, and I'll show you have to actually do that or you could also search for it yourself. So to change this to an integer, we can use an int function, and I'll go into the Python console quickly to just you how it works. So, it can take anything, so like say "false", and produce an int, which is zero. If I take a string of 23, it'll produce 23. As an integer. And then if I do int, say 23 + 5, it should give me 28. Now we can also, just for your reference, we can go and actually cast this to a string instead, so if we took the string 23 and added the string 5, we would now get a string of 235, because the way that addition works in strings and in integers are different. So now, if we wanna debug this, and if we didn't have this handy Pycharm warning, I could also try to figure out what's going on by adding a print statement here. Which I'll remove later. And just checking what the temp F is. What's the type of temp F variable is. And so I can run this. And it says, it's a "class str". So yes, we want to convert it to an integer first before we start messing around with it as a number. So I'll delete this. And, I can now wrap this in a function, in line, or I can say "temp F = int Temp F" here. That's maybe a little easier to read for now. I'll try again. Oh and it's saying "Temp in C + temp C". So we're getting a new error, so that's cool. I can print what temp C is now. And it's -5.0, but now I'm getting the opposite error, it's saying that now I'm trying to concatenate a string, to a float, and it's also not happy. It's a different type error. Oops! I'll keep that there and then, so I can do the same thing and cast this one to a string now. Okay. So if it's 23 degrees Fahrenheit, it's negative five Celsius. And, I was just, let's see, what is sixty degrees Fahrenheit? That is, 15.6 degrees Celsius. Yeah So, that's a little bit about how to debug things. One other feature of Pycharm, so the besides print statements, you can actually use a debugger. And Pycharm has a debugger built in and if I click on, between the line number and the code, you can see this little like red dot pop up. So if I actually run this in debug mode, so instead of run, I have debug and there's a little bug here. Then, it'll run until it hits this, what's called a break point. And then it'll stop and tell me a bunch of information about the status at that point. So I'm gonna give a temperature in Fahrenheit and then now it stops, and I can see temp F = 60, and if I wanted to, I could open up this little expression calculator, and so I could be like, "what is the type of temp F?" Evaluate, oh it's an integer. That's cool. What happens if I cast it to a string? Now it becomes the string 60. So, yeah. This is also a handy way of being able to debug your code, and that's a very useful skill to be able to have, and arguably more useful than being able to memorize like, zintacks and stuff. Because we have all these tools to help you with that now. So, I can also click this to resume the program and then it'll continue running the rest of it and print out whatever it needs to print out in the console. So yeah, Python errors are just like error signs in real life, they're telling you "hey, you're doing something wrong" and I'm not going to keep going. In Pycharm, it's handy that it also has the red color, of something's wrong here, so it's easy to see. And it also has that nice trace back, where you can find out what part of the code is the problem. Also, if you're curious about the different kind of errors that python throws, we can look up python type error. You know, maybe we just want to know, what does Python mean when they throw that type error? And, one of the things that comes up is this documentation. So, this is the documentation for Python 2.7, this is 3.72, um, so I'll look at this one. You can change the version up here. And I'll just find type error. Nah, that's not it, I'll click on that. Here we go. Exception type error, and then it gives you a whole bunch of information about when and why it gets thrown. So this is the official Python documentation, this is the first time we are looking at it, but we will refer to it in the future for other things related to the python language.