Explaining Errors & Solutions — Guided Lab
Scenario:
You ran your Python script and got an error message. You’re not sure what it means or how to fix it. Your AI assistant can interpret the error, explain the cause, and suggest solutions — acting like a personal debugging tutor.
Step 0: Identify the Problem
Example Python function:
def divide(a, b):
return a / b
Run:
print(divide(5, 0))
Error received:
ZeroDivisionError: division by zero
Goal: Understand the error and fix the function safely.
Step 1: Craft the AI Prompt
Treat AI as a debugging tutor. Include:
- The code snippet
- The exact error message
- Request for explanation and possible solutions
Example Prompt:
I ran the following Python function:
def divide(a, b):
return a / b
It throws this error: ZeroDivisionError: division by zero
Please explain what this error means and suggest a safe fix.
Step 2: AI Output Example
Explanation:
ZeroDivisionErroroccurs when you try to divide a number by zero.- Python cannot perform this operation, so it raises an exception.
Suggested Fix:
def divide(a, b):
if b == 0:
raise ValueError("Denominator cannot be zero")
return a / b
Test:
print(divide(10, 2)) # 5.0
# print(divide(5, 0)) # Raises ValueError with descriptive message
Step 3: Mini Lab Challenges
- Write a function
sqrt(n)and handle negative input errors. - Trigger and fix a
TypeErrorfor a function that expects integers but receives strings. - Provide AI with a traceback from a complex script and ask for a step-by-step explanation.
- Ask AI to suggest multiple alternative solutions for a common Python error.
Step 4: Pro Tips
- Always include the full error message and code snippet in the prompt
- Ask AI for multiple solutions to understand different approaches
- Use AI explanations to learn Python exceptions and best practices
- Iteratively test AI suggestions to ensure they solve the problem safely
Lab Summary
- AI can act as a personal debugging tutor, explaining errors in plain language
- Clear prompts + full error context = actionable solutions
- Understanding AI’s explanation improves your problem-solving skills
- Combining AI with hands-on testing reduces frustration and accelerates learning






