Errors are bound to happen in your programs. In this video learn about the different types of errors and how you can troubleshoot them.
- One of my favorite quotes about programing is from Alan Perlis, who said, "There are two ways to write error free programs, "only the third one works." This play on words is meant to highlight the fact that programs tend to have bugs, and you're not doomed as a programmer if you run into issues when writing and running your code. But when you're first getting started, what can you do to figure out why your code is acting up? Well, first, you need to understand what type of bug or error you're dealing with. Errors tend to fall into three categories. Syntax, you somehow broke the rules of the language. Runtime, the computer was unable to execute a portion of your code. And finally semantic, the output of the program is not what you expected. Let's look at an example of each type of error. We'll go to the command line and start our Python Shell. We'll start out by typing print, and then hello world. And finally enter. Uh-oh, we didn't get our familiar hello world like we expected. This is because the quotes were missing, the Python interpreter didn't understand what we wanted it to do. When you come across these types of errors, it's best to slowly review each line of code to see where you have missed providing a set of quotes, parentheses or a colon. The next type of error is a runtime error. This happens when your program is actually running. Let's look at an example. We're going to create a new statement, this is going to use the expression 10 times two divided by zero. Now let's hit enter, and we receive a zero division error. But why? This is because division by zero was attempted. From mathematics, we know that division by zero is undefined. The Python interpreter has no idea what to do with that expression, so it just fails. When you come across runtime errors that you don't understand, the best thing to do is look it up. My preferred website for doing so is stackoverflow.com. You can copy and paste the error message and then look for similar questions to what you're experiencing. When you click on the question, you'll often find a suitable answer. Now, the final type of error is a semantic one. This is when the output of the program is not what you expected. Let's take a look at an example. We're going to print out hello Alice. We'll start by creating a variable called name, and we'll set it equal to Alice. And hit enter. Next, we're going to use our print statement, and we're going to include hello and then name, and enter. Oh no, look what happened, we have hello name instead of hello Alice. That's because we didn't use the proper variation of the print method. Python didn't understand that we wanted it to use the name variable. Semantic errors are the most challenging to troubleshoot, it's best to make tiny changes and then run your code. That makes it easier to figure out where you introduced the error. Also, I find going for a short walk helps me to clear my head and come back to the problem with fresh ideas. Ultimately, you need to have a good sense of humor. Sometimes these issues pop up from the smallest of mistakes, just laugh it off and keep moving forward. It's all part of the process.
- Writing source code
- Basic statements and expressions
- Working with numbers and strings
- Writing conditional code
- Increasing your efficiency with functions
- Working with comments
- Making decisions in code
- Troubleshooting errors
- Learning about other languages