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
runtime errors
logic 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? →
unbalanced quotation marks: print("hello world)
an invalid variable name: $my_var = "foo"
etc.
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? →
adding a string to an int: print(5 + "foo")
dividing by 0: print(5 / 0)
etc.
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. →
Ok. So How Do We Fix Errors?
don't make mistakes in the first place :P (obvs… but how?)
debugging
How to Avoid Bugs
What are some ways of avoiding bugs? →
careful planning and design
set small, incremental goals for your program (don't try and write large
programs all at once)
stop and test your work often as you go
use comments to ignore lines that are giving you trouble, but you want to save for later (let's see this in action based on our previous code →)
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)? →
go through your code line-by-line
want to know what's going on? print it out?
use the debugger
use the interactive shell
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!
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 Stuff Out!
Print out the values of variables to help you isolate where the issue is:
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.
enable the debugger window by clicking on the interactive shell window first
Debug → debugger
make sure that all four checkboxes are checked: stack, locals, source and globals
run your program (your program will pause immediately)
then, continually click on "Over" to step through our program line-by-line (we won't be using step or out yet)
note the values of the variables on the bottom (and the line of code on the top)
(Go and quit stop your debugging session)
Python Tutor
Python Tutor is similar to IDLE's debugger, but you access it online.