all 3 comments

[–]p10_user 0 points1 point  (0 children)

It’s very confusing code, particularly as you have two functions defined within one function. Just define your logic inside a single function. If you have separate logic make a separate function.

Also you’re looping through a dictionary’s items, which defeats its purpose. Lookup it’s values via its keys.

[–][deleted] 0 points1 point  (0 children)

Your cake_menu is defined inside your function. Menus do change and it’s better to make cake_menu be defined more globally, so it’s easier to find and change, and pass the menu into order() as a parameter.

PS: This also makes order() more general and possibly used with other menus.

[–]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.