Practice: Debugging Fundamentals

« Read the chapter

Q1. Catch the zero division

BeginnerCoding

Write code that divides 10 by 0 inside try and prints the name of the exception type.

Goal: Identify runtime errors.

Q2. Assert a positive number

BeginnerCoding

Write assert n > 0 for n = -3 inside try, and print the message must be positive when it fails.

Goal: Use assert.

Q3. Show hidden spaces

BeginnerCoding

Print the repr of the string "hi \n" (hi, a space, a newline).

Goal: Use repr to inspect.

Q4. Fix the off-by-one

IntermediateCoding

Print the numbers 1 to 5, each on its own line, using range correctly.

Goal: Avoid off-by-one errors.

Q5. Safe default list

IntermediateCoding

Write a function add(x, items=None) that returns a list with x added, creating a new list when none is given. Print add(1) and add(2).

Goal: Avoid mutable defaults.

Q6. Log at two levels

IntermediateCoding

Configure logging to stdout with the format LEVEL: message at level INFO. Log one DEBUG and one INFO message, so only the INFO line appears.

Goal: Use logging levels.

Q7. Find the first bad item

AdvancedCoding

Given [3, 5, -1, 8, -2], print the index of the first negative number using a loop.

Goal: Locate a faulty value.

Q8. Mutable default surprise

IntermediateOutput

What does this print?

Code
def f(x, items=[]):
    items.append(x)
    return items

print(f(1))
print(f(2))

Goal: Predict the mutable default trap.

Q9. Range end

BeginnerOutput

What does this print?

Code
print(list(range(1, 5)))

Goal: Read range bounds.

Q10. Repr of a string

BeginnerOutput

What does this print?

Code
print(repr("Ana "))

Goal: Use repr.

Q11. Modifying while looping

AdvancedOutput

What does this print?

Code
nums = [1, 2, 3, 4]
for n in nums:
    if n % 2 == 0:
        nums.remove(n)
print(nums)

Goal: Trace list changes during iteration.

Q12. Assertion message

IntermediateOutput

What does this print?

Code
x = 1
try:
    assert x > 5, "too small"
except AssertionError as e:
    print("caught:", e)

Goal: Read assertion output.

Q13. Integer or string

BeginnerOutput

What does this print?

Code
value = "5"
print(value * 2)

Goal: Spot type bugs.

Q14. Which line fails

IntermediateOutput

What does this print?

Code
try:
    int("3.5")
except Exception as e:
    print(type(e).__name__)

Goal: Predict exception types.

Q15. Wrong average

BeginnerDebugging

This function gives 6.0 for [2, 4, 6] but should give 4.0. Fix it.

Code
def average(nums):
    return sum(nums) / (len(nums) - 1)

print(average([2, 4, 6]))

Goal: Fix a logic error.

Q16. Missing colon and typo

BeginnerDebugging

This code has a syntax error and a wrong name. Fix it so it prints Big.

Code
size = 10
if sise > 5
    print("Big")

Goal: Fix syntax and name errors.

Q17. Skipped items

AdvancedDebugging

This loop should remove all even numbers but skips some. Fix it by building a new list. It should print [1, 3, 5].

Code
nums = [2, 4, 1, 3, 6, 5]
for n in nums:
    if n % 2 == 0:
        nums.remove(n)
print(nums)

Goal: Avoid editing a list while looping.

Q18. Three kinds of errors

BeginnerConceptual

Name the three kinds of errors and give a short example of each.

Goal: Classify errors.

Q19. Assert versus if

IntermediateConceptual

Why should user input be checked with if and not with assert?

Goal: Know the role of assert.

Q20. Trace a bad discount

IntermediateApplication

A shop function discount(price, percent) should return price minus the percent. Write it and print discount(200, 10). It should print 180.0.

Goal: Apply careful reasoning to a calculation.