Courseiva

PCEP Practice Question: Functions, Tuples, Dictionaries and Exceptions

A developer writes a function to safely divide two numbers and wants to explicitly raise a ValueError when the divisor is zero, rather than letting a ZeroDivisionError propagate. The function signature is `def safe_divide(a, b):`. Which code snippet correctly accomplishes this?

⚠ Common exam trap

Test-takers frequently confuse assert with raise, or assuming that catching ZeroDivisionError and re-raising ValueError is equivalent to explicitly raising before division.

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

✓

if b == 0: raise ValueError('Cannot divide by zero') then return a / b

The correct approach checks the divisor explicitly and raises a ValueError with a clear message before any division occurs. This prevents the built-in ZeroDivisionError and gives the caller a domain-specific exception. The other options either use the wrong exception type, rely on assertions that can be disabled, or contain syntax errors that prevent execution.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • ✗

    if b = 0: raise ValueError('Cannot divide by zero') then return a / b

    Why it's wrong here

    The comparison uses a single equals sign, which is an assignment statement, not an equality check. This causes a SyntaxError in Python. Even if corrected to ==, the logic would still need to be placed correctly before division, but as written, this snippet is invalid and cannot fulfill the requirement.

  • ✗

    try: return a / b except ZeroDivisionError: raise ValueError

    Why it's wrong here

    This catches ZeroDivisionError and re-raises a ValueError, but it does not explicitly raise ValueError before division. The scenario asks for explicitly raising when b is zero, not converting an existing exception. Also, the raised ValueError lacks a descriptive message, and the division still occurs, potentially causing performance overhead.

  • ✓

    if b == 0: raise ValueError('Cannot divide by zero') then return a / b

    Why this is correct

    This is correct because it checks the divisor before performing division and explicitly raises ValueError with a custom message when b is zero. This prevents a ZeroDivisionError from occurring and gives the caller a more meaningful exception. The return statement executes only when b is non-zero, ensuring safe division.

  • ✗

    assert b != 0, 'Cannot divide by zero' then return a / b

    Why it's wrong here

    Using assert raises AssertionError, not ValueError, when the condition is false. Additionally, assertions can be disabled with the -O flag, so this is not a reliable way to enforce business rules. The scenario specifically requires raising ValueError, so this approach fails on both exception type and reliability.

About these practice questions

Courseiva writes every PCEP question from scratch — 482 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or dumps. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

JA

Written and reviewed by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

Last reviewed September 2026 · checked against the official Python Institute exam blueprint

This PCEP practice question is part of Courseiva's free Python Institute certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the PCEP exam.