Lab 4: Debugging Lab

Source — https://xkcd.com/1722/

The goals this week are to:

This is not a typical lab where you start working on a new project, the goal here is to gain some basic debugging skills that will make future projects easier for you.  The solutions are provided, yet I encourage you to take the tasks seriously.


Tasks

L1. Reading error messages

Your first task is to read error messages without seeing the code they come from, and try to figure out where and why the error occurred.

Start by copying the following file to your lab directory and answer the questions below each error message.  [THE FILE]

Now that you tried answering the questions, take a look at the answers. [ANSWERS]


L2. Good code organization

Copy the code below and save it with the name “lab4-task2.py” on your computer:

import turtle

x = turtle.Turtle()

y = turtle.getscreen()

x.speed(0)

y.bgcolor("black")

x.color("white")

x.forward(1)

pen_size = 1

for z in range(300):

    x.forward(z*1.5)

    x.left(91)

    pen_size*=1.004

    x.pensize(pen_size)

    if z<101:

        tammy= ((z/100),0,0)

    elif z<201:

        tammy =(1-((z-100)/100),((z-100) / 100), 0)

    else:

        tammy = (1- ((z - 200) / 100), 1 - ((z - 200) / 100), ((z - 200) / 100))

    x.color(tammy)

x.forward(300 * 0.5)

turtle.hideturtle()

x.hideturtle()

turtle.exitonclick()

Run the code to generate the following shape:

In this task, you are given a working code (the file “lab4-task2.py”), but the code is written with total disregard to coding style 💔​😭​.  Your job is to fix the coding style by editing the code, keeping in mind that the code runs perfectly and the only thing needed is to update the code with coding style concepts like:

  1. Every file should start with a docstring (the date, file, name, and description on top).
  2. A variable should be given a name that describes the data it holds.
  3. Blocks of code should be separated by vertical spaces.
  4. Inline comments (#) should be added before each block to describe it.
  5. Avoid “magic numbers” in your code by using variables. “A magic number is a number in the code that seems arbitrary and has no context or meaning.  This is considered an anti-pattern because it makes code difficult to understand and maintain.  One of the most important aspects of code quality is how it conveys intention. Magic numbers hide intention so they should be avoided.” [source]
  6. Surround operators by spaces, for example: var1 = var2 instead of var1=var2.
  7. Avoid extra long lines of code.
  8. Any other coding style tips that you know.

For a full (and very long) list of tips on good coding style check: https://pep8.org/

You can find the “solution” for this task at the following link: [SOLUTION]

The main point from this task is to recognize how difficult it is to modify and debug code with bad style even if it works perfectly.  This is a short program, imagine how difficult it would be to modify a larger code or debug it if it does not follow a consistent coding style.


L3. Debugging puzzle

In the final task of this lab, you will use everything you learned to fix the coding bugs in the following files: compound_tree.py and shapelib.py 

The code is supposed to generate the following shape (if you fix all bugs):

The solution is available through this link: [SOLUTION]

Good job on fixing all the bugs and learning about the importance of good coding style and how to debug code! 👏👏👏


Let’s work on project 3 when you are done!