Skip to content

Instantly share code, notes, and snippets.

@brianspiering
Created December 4, 2021 01:38
Show Gist options
  • Save brianspiering/59c96adf5bff0f44bb6186d9aca0dd6e to your computer and use it in GitHub Desktop.
Save brianspiering/59c96adf5bff0f44bb6186d9aca0dd6e to your computer and use it in GitHub Desktop.
Debugging advice for programming

What to do when your code doesn't work

If you have problems, follow these steps:

  1. Don't Panic! Relax and realize that you will solve this problem, even if it takes a little bit of messing around. Banging your head against the computer is part of your job. Remember that the computer is doing precisely what you tell it to do. There is no mystery.

  2. Determine precisely what is going on. Did you get an error message from Python? Is it a syntax error? If so, review the syntax of all your statements and expressions.

  3. If you got an error message that has what we call a stack trace, a number of things could be wrong. You read a stack trace from bottom-to-top. Go slowly and understand each character and each line.

    Given this Traceback:

    "abc".uppre()
    ---------------------------------------------------------------------------
    AttributeError                            Traceback (most recent call last)
    <ipython-input-2-a2268f9e2941> in <module>
    ----> 1 "abc".uppre()
    
    AttributeError: 'str' object has no attribute 'uppre'

    I misspelled upper() as uppre().

  4. If it does not look like it some simple misspelling, you might get lucky and find something in Google if you cut-and-paste that error message.

  5. If your code is running without errors, then insert print statements to figure out what the variables are at different points. That often tells you what the problem is. Something like works well - print(f"Expected {foo}. But actually got {bar}.").

  6. You will the most by trying to solve the problem yourself, but don't waste too much time (~15 minutes at the upper end). You should then ask someone for help. Options include an instructor, senior team member, peer, or Stack Overflow.

Other resources

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment