Robotics-II-Circuit-Python

Circuit Python tutorials in Robotics II

View the Project on GitHub MrPrattASH/Robotics-II-Circuit-Python

Table of Contents

Introduction to Debugging with Traceback Errors

Video Tutorial


Text Tutorial

When you run a piece of code and encounter an error, you’ll often see a “traceback” in the serial console. This traceback provides valuable information about what went wrong, where it happened, and sometimes hints at why the error occurred.

Understanding Traceback Errors

A traceback error acts like a map leading you to the problem in your code. It usually includes:


Using Tracebacks in Debugging

  1. Read the Error Type: Identifying whether it’s a SyntaxError, NameError, etc., helps narrow down potential fixes.
  2. Locate the Line Number: This tells you exactly where to start looking for the problem.
  3. Understand the Description: Look for hints in the message about what might be wrong.

Common Debugging Strategies

Let’s practice using these strategies with some coding tasks.


Debugging Practice: Task 1 - Syntax Error

Code to Paste:

print("Welcome to CircuitPython")
print("This line is missing something"

Traceback Clue: Look at the serial console for a “SyntaxError” message. This error usually means there’s something structurally incorrect with your code, like a missing parenthesis.

Strategy Hint

# Strategy: Check to ensure every opening parenthesis has a closing pair.
Solution

print("Welcome to CircuitPython")
print("This line is missing something")

Debugging Practice: Task 2 - Name Error

Code to Paste:

animal = "Tiger"
print(animal)
print(animel)

Traceback Clue: The console should display a “NameError,” indicating that a variable is being used before it’s defined or is misspelled.

Strategy Hint

# Strategy: Double-check spelling of variable names; consistent naming is key.
Solution

animal = "Tiger"
print(animal)
print(animal)

Debugging Practice: Task 3 - Undefined Variable

Code to Paste:

print(total)
num1 = 5
num2 = 10
total = num1 + num2

Traceback Clue: You might see a “NameError” telling you that the variable total is being referenced before assignment.

Strategy Hint

# Strategy: Ensure all variables are defined before they are used.
Solution

num1 = 5
num2 = 10
total = num1 + num2
print(total)

Debugging Practice: Task 4 - Type Error

Code to Paste:

age = "20"
new_age = age + 5
print("New age:", new_age)

Traceback Clue: Look for a “TypeError,” which happens when you try to perform operations incompatible with the variables’ data types.

Strategy Hint

# Strategy: Ensure that arithmetic operations involve compatible data types. Convert strings to numbers where needed using functions like int().
Solution

age = "20"
new_age = int(age) + 5
print("New age:", new_age)

By practicing with these tasks and learning to decode traceback errors, you’ll build your ability to debug efficiently and write cleaner, more effective code. Remember, debugging is an essential skill to build as a programmer.