๐Ÿœ๏ธ SGCA Year 6 โ€” Coding Reference Sheet

Debugging Dunes ยท Debugging & Testing ยท Python 3

Canon Authority: Katarzyna Kalina vel Kalinowska

v3.0 MASTER CANON LOCKED

๐Ÿ”ด๐ŸŸ ๐ŸŸฃ Three Error Types
TypeWhat happensExample
SyntaxWon't runMissing colon
LogicWrong answer= instead of ==
RuntimeCrashesDivide by zero
Syntax errors โ€” common causes:
# Missing colon if x > 0: # โ† needs colon for i in range(5): def greet(name): # = vs == if x == 5: # comparison x = 5 # assignment # Unclosed quote print("hello") # โ† both quotes
โš ๏ธ Python tells you the LINE NUMBER in error messages!
๐Ÿ” The 5-Step Debug Process
1. READ โ€” Read the error message 2. LOCATE โ€” Find the line with the bug 3. UNDERSTAND โ€” Why is it wrong? 4. FIX โ€” Make ONE change 5. TEST โ€” Run again, verify
Debugging questions to ask:
# What SHOULD happen? # What DOES happen? # Where do they differ? # What changed recently? # Is the logic correct?
๐Ÿ’ก Fix ONE thing at a time โ€” then test!
Print debugging:
print("DEBUG: x =", x) print("DEBUG: list =", my_list) print("DEBUG: reached line 15")
๐ŸŸฃ Runtime Errors
ErrorCause
ZeroDivisionErrorx / 0
NameErrorUndefined variable
IndexErrorlist[10] (too big)
ValueErrorint("abc")
TypeError"a" + 5
AttributeErrorWrong method
Reading error messages:
Traceback (most recent call last): File "main.py", line 5, in <module> result = 10 / 0 ZeroDivisionError: division by zero # โ†‘ Error type โ†‘ Description # Line 5 is where the error is!
๐Ÿ›ก๏ธ try / except
Basic structure:
try: result = 10 / 0 except ZeroDivisionError: print("Cannot divide by zero!")
Multiple except blocks:
try: x = int(input("Enter number: ")) result = 100 / x print(result) except ValueError: print("Not a valid number!") except ZeroDivisionError: print("Cannot divide by zero!")
With else and finally:
try: result = 10 / 2 except ZeroDivisionError: print("Error!") else: print("Success:", result) # no error finally: print("Always runs")
โš ๏ธ Only catch errors you expect!
๐Ÿงช Writing Tests
Simple test cases:
# Test: is_even() print(is_even(4) == True) # True print(is_even(7) == False) # True print(is_even(0) == True) # True
Using assert:
assert is_even(4) == True assert is_even(7) == False assert get_grade(95) == "A" assert get_grade(85) == "B" print("All tests passed! โœ…")
Test types to include:
TypeExample
Normal caseis_even(4) โ†’ True
Edge caseis_even(0) โ†’ True
Boundaryget_grade(90) โ†’ "A"
Negativeis_even(-2) โ†’ True
๐ŸŽฏ Edge Cases & Boundaries
Common edge cases:
SituationEdge case to test
List functionEmpty list []
Number functionZero, negative
String functionEmpty string ""
DivisionDivisor = 0
IndexFirst and last item
Boundary testing example:
# Grade: A = 90+, B = 80-89 get_grade(90) # A (at boundary) get_grade(89) # B (just below) get_grade(80) # B (at boundary) get_grade(79) # C (just below) get_grade(0) # F (minimum) get_grade(100) # A (maximum)
๐Ÿ’ก Test the EXACT boundary value AND one above/below!
๐Ÿ”ง Logic Error Checklist
CheckCommon mistake
Operators< vs >, == vs !=
ValuesWrong return value
Case"Nova" โ‰  "nova"
Off-by-onerange(1,10) misses 10
AccumulatorStart at 0 vs 1
Condition orderMost specific first
Trace technique:
# Add print statements to trace def get_grade(score): print(f"DEBUG: score={score}") if score >= 90: print("DEBUG: returning A") return "A" # etc...
๐Ÿ’ก Trace = follow code step by step manually
๐Ÿ† XP Ranks & Worlds
RankXPIcon
Cadet0๐ŸŒฑ
Explorer500๐Ÿ”ญ
Navigator1,500๐Ÿงญ
Captain3,000โšก
Admiral6,000๐Ÿ‘‘
WorldYearTheme
๐ŸŒ Syntax World3Sequences
๐Ÿ”ฎ Algorithm Alley4Logic
๐ŸŒ€ Syntax Station5Loops
๐Ÿœ๏ธ Debugging Dunes6Debugging
๐ŸŽจ Creativity Cluster7Creative
๐Ÿ”’ Canon Authority: Katarzyna Kalina vel Kalinowska