Home > Enterprise >  Error when trying a function that returns the total quantities in the storage unit
Error when trying a function that returns the total quantities in the storage unit

Time:11-28

The goal is to write a function that returns the total quantity of all storage units put together (Madrid, Barcelona and Seville), I do think its better to use a recursion for this problem however i cant seem to work it out! I have this dictionary:

Storage = {
    "Madrid": [
        {"name": "pencil", "quantity": 5},
        {"name": "cam", "quantity": 11},
        {"name": "powder", "quantity": 51}
    ],
    "Barcelona": {
        "Branch 1": [
            {"name": "pencil", "quantity": 11},
            {"name": "cam", "quantity": 25}
        ],
        "Branch 2": [
            {"name": "pencil", "quantity": 17},
            {"name": "cam", "quantity": 9}
        ]
    },
    "Seville": {

        "Branch 1": {
            "Sub Branch 1": {
                "Sub sub Branch 1": [
                    {"name": "powder", "quantity": 11}
                ]
            }
        },
        "Branch 2": [
            {"name": "pencil", "quantity": 4}
        ]

    }
}

I searched and wrote a lot of codes and this is the one that made the most sense

def recursive_sum(n):
    current_sum = 0
    for key in n:
        if not isinstance(n[key], dict):
            if not isinstance(n[key], str):
                current_sum = current_sum   n[key]
        else:
            current_sum = current_sum   recursive_sum(n[key])
    return current_sum


print(recursive_sum(Storage))

but it returns this:

Traceback (most recent call last):
  File "/Users/user/Desktop/pythonProject/main.py", line 85, in <module>
    print(recursive_sum(Storage))
  File "/Users/user/Desktop/pythonProject/main.py", line 79, in recursive_sum
    current_sum = current_sum   n[key]
TypeError: unsupported operand type(s) for  : 'int' and 'list'

i searched a lot but i cant seem to understand how am i going to take the values of the list inside the dictionary, am i thinking wrong? Thank you in advance!

CodePudding user response:

I see two thing going wrong here:

  1. You never check for the type "list", which you would need to iterate over
  2. Once you are iterating over the list, you will get dictionaries again, of which you need to extract the "quantities" before you try to sum them.

I would approach it differently: create an empty output dictionary. Then dig into the source (iterating roughly as you do), and check whether the "quantity" key exists inside the current level. Then check if the corresponding key exists, and add the quantity to the result dictionary.

CodePudding user response:

Your functions fails when it gets here

if not isinstance(n[key], str):
            current_sum = current_sum   n[key]

and n[key] is :

[{'name': 'pencil', 'quantity': 5}, {'name': 'cam', 'quantity': 11}, {'name': 'powder', 'quantity': 51}]

This can be fixed with a simple loop

def recursive_sum(n):
current_sum = 0
for key in n:
    if not isinstance(n[key], dict):
        if not isinstance(n[key], str):
            for i in n[key]:
                current_sum = current_sum   i["quantity"]
                
    else:
        current_sum = current_sum   recursive_sum(n[key])
return current_sum
  • Related