Circuit Python tutorials in Robotics II
View the Project on GitHub MrPrattASH/Robotics-II-Circuit-Python
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.
A traceback error acts like a map leading you to the problem in your code. It usually includes:
Let’s practice using these strategies with some coding tasks.
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: Check to ensure every opening parenthesis has a closing pair.
print("Welcome to CircuitPython")
print("This line is missing something")
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: Double-check spelling of variable names; consistent naming is key.
animal = "Tiger"
print(animal)
print(animal)
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: Ensure all variables are defined before they are used.
num1 = 5
num2 = 10
total = num1 + num2
print(total)
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: Ensure that arithmetic operations involve compatible data types. Convert strings to numbers where needed using functions like int().
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.