Errors and Debugging

Ugh. My Program Doesn't Work

Types of Errors

Let's take a look at some things that may prevent your program from working.

Based on what we've seen in the previous classes, the online module, and (hopefully) homework #1, what are some mistakes / errors that you can make while programming?

Can we categorize these errors?

Syntax Errors

A syntax error is an error caused when your program does not follow the rules that define the allowable combinations of characters/symbols for a correctly written program in a particular language.

If you have a syntax error, your program cannot even be executed because the Python interpreter can't understand your program! What are some examples of syntax errors?

Runtime Errors

A runtime error is an error that occurs while a syntactically correct program is running. Runtime errors will cause your program to stop (crash) if you do not have special code to handle them.

What are some examples of runtime errors?

Logic Errors

A logic error is in an error that occurs in program that is syntactically correct, does not contain runtime errors… but behaves in a way that is unintended or unanticipated. That is, it does not achieve the goals of what the program is meant to do.

Find the logic error below.

#  ask the user for three integers
#  print out the sum of all three integers
num1 = int(input("Give me the first integer\n> "))
num2 = int(input("Give me the second integer\n> "))
num3 = int(input("Give me the third integer\n> ")) 

print(num1 + num2 + num2)

Ok. So How Do We Fix Errors?

  1. don't make mistakes in the first place :P (obvs… but how?)
  2. debugging

How to Avoid Bugs

What are some ways of avoiding bugs?

Debugging Overview

What are some ways that you can isolate and find the root cause of errors (we know a couple, we'll introduce a few more)?

  1. go through your code line-by-line
  2. want to know what's going on? print it out?
  3. use the debugger
  4. use the interactive shell
  5. use python tutor

Working Through Code Line-by-Line

This is the simplest, and most important way of finding bugs. Just read your code!

Reading through this code carefully would reveal the error!

#  ask the user for three integers
#  print out the sum
num1 = int(input("Give me the first integer\n> "))
num2 = int(input("Give me the second integer\n> "))
num3 = int(input("Give me the third integer\n> ")) 

print(num1 + num2 + num2)

This is the most important method of debugging because this helps increase your code comprehension skills. Also, you won't have a computer with you during the exam.

Print out the values of variables to help you isolate where the issue is:

<pre><code data-trim contenteditable>
num1 = int(input("Give me the first integer\n> "))
num2 = int(input("Give me the second integer\n> "))
num3 = int(input("Give me the third integer\n> ")) 

// let's see what's in the input that we asked for!
print(num1, num2, num3)

print(num1 + num2 + num2)
</code></pre>

We'll see that the input looks correct… so clearly, the issue is in the calculation

Use the Debugger

A debugger is software that lets you step through your code line by line… and it allows you to inspect the values of variables that exist up to a specific line.

Python Tutor

Python Tutor is similar to IDLE's debugger, but you access it online.