Runtime error: divide by zero
A Python runtime error raised when a number is divided by 0, or a remainder is taken with % 0. Guard against it with an if check or try/except.
Understanding Runtime error: divide by zero in VCE Software Development U3O1
What it does: The if statement checks the divisor is not 0, so the division only runs when it is safe. The division is attempted in the try block, and the except block handles ZeroDivisionError and lets the program carry on. Dividing by zero is undefined, so Python stops with a ZeroDivisionError. It does not quietly return 0. Taking a remainder with % 0 is also a divide by zero. 0 % 17 is fine and gives 0 because the divisor is 17. Check that the divisor is not 0 before dividing. The second option is not valid Python syntax. Zero can come from a calculation, not just from user input. Here 5 - 5 is 0, so the division fails.
Common mistakes: The guard is inverted. The division now runs when n is 0 and causes a ZeroDivisionError. It should be if n != 0:. This divides by the constant 0, so it always raises ZeroDivisionError. It should divide by the checked variable n: print(100 / n).
Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.
This module's key terms are also in a printable crossword (Modules 156–166) and word search (Modules 142–166).
Take this worksheet further — free with Cloodit
Each concept is reinforced from every angle — learned, then applied, then tested.
Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.
Over 100 of them, across seven natural biomes.
Turns their work into revision material ready for Flash Cards and Exam study.
Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.
Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.
Quick Questions
Match the Pairs
Key Term
ZeroDivisionErrorFill in the Blanks
Spot the Error
Student with a class code? Join your class →