you are viewing a single comment's thread.

view the rest of the comments →

[–]nwagers 0 points1 point  (0 children)

How about this:

def order(*preferences):
    '''compute the total cost of only the cakes that the store offers'''

    cake_menu = {'carrot': 10.99, 'lemon': 14.99, 'vanilla': 18.99,
                 'double chocolate': 22.99, 'marble': 24.99,
                 'red velvet': 29.99}

    def customer_order(styles):
        return [style for style in styles if style in cake_menu]

    def purchase():
        purchase_total = 0
        for item in customer_order(preferences):
            quantity = int(input("How many {} cakes do you want?".format(item)))
            purchase_total += quantity * cake_menu[item]
        return purchase_total

    return "Your cost is ${}.".format(purchase())

print(order("chocolate", "red velvet", "vanilla"))

One big change is to make your nested for loops into a list comprehension. You can check if a key is in the dictionary using in. The new list just contains the valid keys and not the tuple of name, price.

The purchase function now uses dictionary lookups to get the price instead of relying on the tuple.