Zurück zur Lektion

examples/16.py

Code und ursprüngliche Produktansichten behalten ihre Originalsprache. Die Lektion erklärt sie in deiner gewählten Sprache.

def calculate_total(price, quantity):
    if quantity < 0:
        raise ValueError("quantity must not be negative")
    return price * quantity

def complete_order(price, quantity, send_receipt):
    total = calculate_total(price, quantity)
    send_receipt(total)
    return total

receipts = []
assert complete_order(12, 3, receipts.append) == 36
try:
    complete_order(12, -1, receipts.append)
except ValueError:
    pass
else:
    raise AssertionError("negative quantity accepted")
assert receipts == [36]