Practice: Debugging Fundamentals
Q1. Catch the zero division
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
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
Print the repr of the string "hi \n" (hi, a space, a newline).
Goal: Use repr to inspect.
Q4. Fix the off-by-one
Print the numbers 1 to 5, each on its own line, using range correctly.
Goal: Avoid off-by-one errors.
Q5. Safe default list
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
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
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
What does this print?
def f(x, items=[]):
items.append(x)
return items
print(f(1))
print(f(2))Goal: Predict the mutable default trap.
Q9. Range end
What does this print?
print(list(range(1, 5)))Goal: Read range bounds.
Q10. Repr of a string
What does this print?
print(repr("Ana "))Goal: Use repr.
Q11. Modifying while looping
What does this print?
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
What does this print?
x = 1
try:
assert x > 5, "too small"
except AssertionError as e:
print("caught:", e)Goal: Read assertion output.
Q13. Integer or string
What does this print?
value = "5"
print(value * 2)Goal: Spot type bugs.
Q14. Which line fails
What does this print?
try:
int("3.5")
except Exception as e:
print(type(e).__name__)Goal: Predict exception types.
Q15. Wrong average
This function gives 6.0 for [2, 4, 6] but should give 4.0. Fix it.
def average(nums):
return sum(nums) / (len(nums) - 1)
print(average([2, 4, 6]))Goal: Fix a logic error.
Q16. Missing colon and typo
This code has a syntax error and a wrong name. Fix it so it prints Big.
size = 10
if sise > 5
print("Big")Goal: Fix syntax and name errors.
Q17. Skipped items
This loop should remove all even numbers but skips some. Fix it by building a new list. It should print [1, 3, 5].
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
Name the three kinds of errors and give a short example of each.
Goal: Classify errors.
Q19. Assert versus if
Why should user input be checked with if and not with assert?
Goal: Know the role of assert.
Q20. Trace a bad discount
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.