Seamonsters-2605.github.io

Best Practices for Programming

General tips

Guidelines for writing code

Tips for figuring out why something isn’t working

How to read a stack trace:

Stack traces are printed when an error occurs in your program. They can be long and overwhelming in some cases, but it’s important to understand what they mean. For example, when you run this program:

def a():
    b()
def b():
    print(thisVariableDoesntExist)
a()

You will get this error:

Traceback (most recent call last):
  File "test.py", line 8, in <module>
    a()
  File "test.py", line 3, in a
    b()
  File "test.py", line 6, in b
    print(thisVariableDoesntExist)
NameError: name 'thisVariableDoesntExist' is not defined

The most important part of this error is on the last line. It gives the type of error and extra details about the error. In this case the name is a “NameError,” which according to the Python Documentation occurs when the name of a variable or function is not recognized. Following this is the text name 'thisVariableDoesntExist' is not defined, which pretty clearly explains what went wrong—the variable you were trying to use was not defined.

The text above the bottom line gives more information about where the error occurred, and under what circumstances. The 2 lines above the error show which line the error occurred on, in which function, in which file, and what that line looked like. In this case the error occurred in the file “test.py,” on line 6, in the function b(). Below that is the text of the line (line 6) that caused the error. Above those lines is a list of the series of functions that got you to this place in the code. First the function a() was called on line 8; then, on line 3 in a(), the function b() was called; then, on line 6 in b() the error occurred. When you see <module> in place of a function name, this means the code was outside of a function.