Stuck on Mecha munch management

Hello I’m a beginner in programming and I am need of help.
I’m stuck on task 5 of the Mecha munch management exercise.

The instructions were
" Create the function send_to_store(<cart>, <aisle_mapping>) that takes a user shopping cart and a dictionary that has store aisle number and a True /False for refrigeration needed for each item. The function should return a combined “fulfillment cart” that has (quantity, aisle, and refrigeration) for each item the customer is ordering. Items should appear in reverse alphabetical order."

and I made this code for it.

def send_to_store(cart, aisle_mapping):
    fulfillment_cart = {}
    for item in reversed(sorted(aisle_mapping.keys())):
        aisle_mapping[item].insert(0,cart[item])
        fulfillment_cart.setdefault(item,aisle_mapping[item])
    return fulfillment_cart

and this is the error code I get

I’ve tried enumerating() the function with list in the instructions in pycharm but it returns

“<enumerate object at 0x0000028729741A30>”

I have no idea how to fix it. any ideas on why this is happening?

Thank you.

If you use codeblocks and share the code with spaces and not dashes, others can copy/paste your code to try to reproduce and debug the issue :slight_smile:

Ok! but I have a question what is codeblocks and how do you put it?

edit: I think I figured codeblocks out. Thank you!

You shouldn’t have any > in the codeblocks. Just the ``` before and after the code.

       aisle_mapping[item].insert(0,cart[item])

This assumes the item exists in the cart. If it doesn’t, cart[item] will fail with a KeyError.

1 Like

Understood!

I changed the for statement from statement from

for item in reversed(sorted(aisle_mapping.keys()))

to

for item in reversed(sorted(cart.keys()))

and it worked!

Thank you so much! :smile:

1 Like

Are you now making the reverse assumption that all items in the cart can be found in the aisle?

1 Like

Ah I actually did! Thank you for pointing this out!
Your comment made me realize that if the items in the cart are not in the aisle it would repeat the same problem.

so I came up with this.

def send_to_store(cart, aisle_mapping):
    fulfillment_cart = {}
    for item in reversed(sorted(aisle_mapping.keys())):
        if item not in cart:
            continue
            #or a function that outputs that an item is not found.
        aisle_mapping[item].insert(0, cart[item])
        fulfillment_cart.setdefault(item, aisle_mapping[item])
    return fulfillment_cart

using the ‘continue’ function would skip the missing item and prevent an error if an item is not in the list.

hopefully this time I’m on the right track :crossed_fingers:

That fixes the issue :slightly_smiling_face: Now you should request a code review to further improve it!

2 Likes

Thank you!