ወደ ትምህርቱ ተመለስ

examples/16.py

ኮድና የመጀመሪያ የምርት ማያዎች የመጀመሪያ ቋንቋቸውን ይጠብቃሉ። ትምህርቱ በመረጥከው ቋንቋ ያብራራቸዋል።

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]