Kodewick
Start learning →
Basic←→0 / 80

Laundry bill

#050BasicErrorsDicts4 min

The laundry's price list is {item: price}, and you hand over a bag of clothes bag. Write laundry_bill(prices, bag) to add up the price of everything in the bag; anything not on the price list (they don't wash it) is skipped and costs nothing.

Examples
laundry_bill({"shirt": 5, "coat": 12}, ["shirt", "coat", "shirt"])→22
laundry_bill({"shirt": 5}, ["sofa"])→0
main.py
def laundry_bill(prices, bag):
    total = 0
    for item in bag:
        try:
            # your code here: look up this item's price and add it to total
            pass
        except KeyError:
            pass
    return total
Ctrl + Enter runs too
Result

Run tries the examples. When they look right, Submit.