0

I am trying to pass a dictionary to a function so that it is the function's first parameter and that type checking occurs to verify that a dictionary has indeed been submitted.

fridge = {"cheese":10, "milk":11, "feta":12, "cream":21, "onion":32, "pepper":14}

def fridge_validation(fridge):
    if not isinstance (fridge,dict) :
        raise TypeError: ("require a valid dictionary to be submitted!")

I thought the following would work....

def dummy (fridge):

return fridge

test_of_dummy=dummy ({"cheese", "milk", "eggs"})

print (test_of_dummy)

{'eggs', 'milk', 'cheese'} (this was what was printed)

Not quite sure if I have done it correctly? Also.... I am confused by the following.

 def dummy (fridge):
    fridge={}
    return fridge





  test_of_dummy=dummy ({"cheese", "milk", "eggs"})
    print (test_of_dummy) "{}" was outputted...

.

but I thought I had already passed on the variables...? So why is the {} seemingly taking precedence over the test_of_dummy ?

As to what I am trying to do....

1) Pass a dictionary called fridge as the 1st parameter to a function. Use isinstance and type error to confirm the dictionary is indeed a dictionary.

2) Have a second function that will subtract from the fridge dictionary

0

2 Answers 2

1

In order to validate your fridge variable you can follow this example:

fridge = {"cheese":10, "milk":11, "feta":12, "cream":21, "onion":32, "pepper":14}

def fridge_validation (fridge = {}):
    # Test if fridge is a dict type
    if not isinstance(fridge, dict):
        # if fridge isn't a dict type, throw an exception
        raise TypeError("require a valid dictionary to be submitted!")
    # if fridge is a dict type return it. So you got your validation
    else:
        return fridge

validation = fridge_validation(fridge)
print(validation)

Output:

{'milk': 11, 'feta': 12, 'onion': 32, 'cheese': 10, 'pepper': 14, 'cream': 21}

However, {"cheese", "milk", "eggs"} is a set type and not a dict type.

You can verify it by using Python interpreter:

>> a = {"cheese", "milk", "eggs"}
>> type(a)
<class 'set'>

So as you see, {"cheese", "milk", "eggs"} is a set not a dict. So if you pass it into your fridge_validation() your code will throw an exception.

Also, in your method:

def dummy(fridge):
    fridge = {}
    return fridge

The return will be always equal to {} which is an empty dict type the cause is your code will always overwrite your fridge value by an empty dict.

An answer to your second question.

How can you subtract values from your dict ? The answer is simple:

I suppose you got a dict after validating it with your fridge_validation() method. For example:

validation = {"cheese":10, "milk":11, "feta":12, "cream":21, "onion":32, "pepper":14}

So:

print(validation["cheese"])

output:

10

also:

print(validation["milk"])

output:

11

And so on for the rest. In summary, in order to substract values from a dict you can use: dictionary["key"] this will output the key's value.

Also, i recommend you reading this tutorial in order to understand how you can deal with python dicts.

Sign up to request clarification or add additional context in comments.

Comments

1

Note: {"cheese", "milk", "eggs"} is a set.

In your first function, you just return the argument, so it's no surprise what you got. In the second one, you set fridge to an empty set before returning it, so you get an empty set returned.

All this seems rather unnecessary though, what exactly are you trying to do, and what do you mean you can only manipulate the dictionary once?

3 Comments

I apologise for saying I can only manipulate the dictionary only once. That was a mistake. In essence, I had written a function to take the fridge dictionary, delete a key. I got the two programs muddled, my bad. its 4 AM here. As to what I am trying to do.... 1) Pass a dictionary called fridge as the 1st parameter to a function. Use isinstance and type error to confirm the dictionary is indeed a dictionary. 2) Have a second function that will subtract from the fridge dictionary.
@Iluvavatar, you stated that {"cheese", "milk", "eggs"} is a set, is that because I did not have a some_name= preceding it?
{"key1": "value1", "key2": "value2"} is a dict because you have keys and values in there. {"value1", "value2"} is a set because there are no keys, just values.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.