Lesson 6 — Spot the bug: the empty list
The case nobody asked about
AI answers usually work for the example in your prompt — and often break on the inputs nobody mentioned. The classic one is the empty list:
sum([]) / len([]) # ZeroDivisionError: division by zero
max([]) # ValueError
Before trusting a function, ask: what happens with nothing? An empty list, an empty string, zero, None.
Then decide what the function should do and write it down: here, the average of no scores is None ("no data"), not a crash and not 0 (which would look like a real score).
if not items: # True for an empty list
return 0 # or whatever "no answer" should be
Why it matters: real data is messy. A new user has no scores yet, and "crashes for new users" is a very bad first impression.
Fix both AI functions so that an empty list returns None instead of crashing. The other results must stay the same.
Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.
A quick 3-question quiz on this lesson — instant feedback.