Solution 1

def divide(a, b):
    if b == 0:
        raise ValueError()

    return a / b

Breaking Math

Complete the function divide, which takes two parameters a and b and returns the division: a / b. There's one special case: if the parameter b is 0 you should raise a ValueError exception. You could implement it either using a try/except or just an if statement. If you have 2 minutes to spare, try both solutions!


divide(4, 2)  # 2
divide(8, 2)  # 4
divide(2, 0)  # ValueError Exception raised!

Test Cases

test division works - Run Test

def test_division_works():
    assert divide(10, 2) == 5

test division by zero - Run Test

import pytest

def test_division_by_zero():
    with pytest.raises(ValueError):
        divide(10, 0)
def divide(a, b): return a / b