Year Group: Year 6 ยท Ages 10โ11
Theme: Error Types ยท Debug Process ยท try/except ยท Testing
Missions: 10 Workbook Missions ยท 16 Game Missions
Language: Python 3
The Three Error Types ยท The Debug Process
Welcome to Debugging Dunes, Admiral! Here every bug is a lesson and every fix makes you stronger! Debugging is a SUPERPOWER!
In your own words, explain what debugging means:
Name 3 famous bugs in computing history (research if needed):
1.
2.
3.
By the end of Debugging Dunes, I want to be able to:
1.
2.
3.
Three types of errors: Syntax โ Python won't run it. Logic โ wrong answer. Runtime โ crashes! Know them all โ fix them all!
Python won't run the code at all. Like a grammar mistake in English.
Examples: missing colon, = instead of ==, unclosed quote
Code runs but gives wrong answer. Hardest to find!
Examples: wrong operator, wrong condition, case sensitivity
Code crashes during execution. Python gives an error message.
Examples: division by zero, index out of range, NameError
Write S (Syntax), L (Logic), or R (Runtime) for each:
| Code | Type (S/L/R) | Why? |
|---|---|---|
if score > 80 (missing colon) | ||
score = "100" then score + 50 | ||
print(undefined_variable) | ||
if score = 80: | ||
list[10] when list has 5 items | ||
while True: print("hello") | ||
10 / 0 | ||
"Nova" == "nova" returns False (wrong) |
What does each error message tell you? Write the fix:
Problem:
Fix:
Problem:
Fix:
Problem:
Fix:
The 5-step debug process: READ โ LOCATE โ UNDERSTAND โ FIX โ TEST. Follow it every time โ it works!
| Step | Your Answer |
|---|---|
| 1. READ โ What is the output? | |
| 2. LOCATE โ Which line has the bug? | |
| 3. UNDERSTAND โ Why is it wrong? | |
| 4. FIX โ Write the corrected line: | |
| 5. TEST โ What should the output be? |
Syntax Error Hunt ยท Logic Error Detective
Syntax errors are the easiest to find โ Python tells you exactly where they are! Look for: missing colons, wrong operators, unclosed quotes, missing brackets!
Each program has 3 syntax errors. Circle them and write the fixes:
Bug 1:
Bug 2:
Bug 3:
Fixed program:
Check each item when hunting syntax errors:
| Check | What to look for | Example fix |
|---|---|---|
| Colons | After if, for, def, else, elif, while | if x > 0: |
| Operators | = vs == (assignment vs comparison) | if x == 5: |
| Quotes | Opening and closing quotes match | "hello" |
| Brackets | Every ( has a matching ) | print(x) |
| Indentation | Consistent spaces (4 spaces) | 4 spaces per level |
Logic errors are the sneakiest bugs! The code runs but gives wrong answers. Trace through the code step by step โ be a detective!
Each function has a logic error. Find it and write the fix:
Bug:
Fix:
Bug:
Fix:
Bug:
Fix:
| Logic Error Type | Example | How to spot it |
|---|---|---|
| Wrong operator | if x < 0 instead of if x > 0 | |
| Wrong value | return "A" in else branch | |
| Case sensitivity | "Nova" == "nova" is False | |
| Off-by-one | range(1, 10) misses 10 | |
| Wrong formula | total instead of total/len |
try/except ยท Writing Tests ยท Edge Cases
try/except catches runtime errors before they crash your program! Always print a helpful message in the except block!
What does this print?
Without try/except, what would happen?
Add try/except to handle the runtime error in each program:
Add try/except:
Add try/except:
| Error Type | Cause | except clause |
|---|---|---|
| ZeroDivisionError | Dividing by zero | except ZeroDivisionError: |
| ValueError | Wrong type (e.g., int("abc")) | |
| IndexError | List index out of range | |
| NameError | Variable not defined | |
| TypeError | Wrong type operation |
Tests prove your code works! Write them for every function you create. A good test has: input, expected output, and actual output!
Write 5 test cases for is_even():
| Input | Expected Result | Test Code |
|---|---|---|
| 4 | True | print(is_even(4) == True) |
| 7 | ||
| 0 | ||
| -2 | ||
| 1 |
Write test cases for each grade boundary:
| Score | Expected Grade | Test Code |
|---|---|---|
| 95 | A | print(get_grade(95) == "A") |
| 85 | ||
| 75 | ||
| 65 | ||
| 55 | ||
| 90 |
Edge cases are where bugs hide! Test the unusual inputs โ empty list, zero, negative numbers, very large numbers. Test the EXACT boundary values!
For each function, list 3 edge cases to test:
| Function | Edge Case 1 | Edge Case 2 | Edge Case 3 |
|---|---|---|---|
| calculate_average(numbers) | |||
| find_max(numbers) | |||
| is_palindrome(word) |
For get_grade(score) where A = 90+, test the boundaries:
Write a complete test suite for calculate_average() including normal cases, edge cases, and boundary values:
Complete Debug Sessions ยท Master Program
A complete debug session: find ALL bugs, fix them one at a time, write tests to verify. This is professional-level debugging!
This program has 5 bugs (mix of syntax, logic, and runtime). Find and fix them all:
Bug 1 (line __):
Bug 2 (line __):
Bug 3 (line __):
Bug 4 (line __):
Bug 5 (line __):
Write test cases to verify your fixed calculate_stats():
| Input | Expected avg | Expected high | Expected low |
|---|---|---|---|
| [100] | |||
| [0, 100] | |||
| [50, 50, 50] | |||
| [85, 92, 78, 95, 88] |
Use ALL your debugging skills to fix the systems!
DEBUGGING DUNES needs you! Use ALL your debugging skills โ syntax, logic, runtime, testing โ to fix the systems and save the Dunes!
Bug 1:
Bug 2:
Bug 3:
Bug 4:
Bug 5:
Each function has a logic error. Write the fix:
Fix 1:
Fix 2:
Fix 3:
Fix 4:
Fix 5:
Add try/except to each function:
Write tests for calculate_stats([85, 92, 78, 95, 88]):
This program has 4 bugs. Find and fix them all:
Bug 1:
Bug 2:
Bug 3:
Bug 4:
You've mastered error types, the debug process, try/except, and testing!
Creativity Cluster awaits, Admiral! ๐จ
assert
A statement that checks if a condition is True; raises AssertionError if False.
Bug
An error in a program that causes it to behave incorrectly.
Debugging
The process of finding and fixing errors in code.
Edge case
An unusual or extreme input that might cause unexpected behaviour.
except
Catches a specific error type in a try/except block.
Logic error
Code runs but produces wrong results. Hardest to find.
Runtime error
Code crashes during execution (e.g., ZeroDivisionError, IndexError).
Syntax error
Code violates Python's grammar rules; Python won't run it.
Test case
A specific input with an expected output used to verify code works.
Trace
Following code step by step to understand what it does.
try
Attempts code that might raise an error.
ZeroDivisionError
Runtime error when dividing by zero.