Dictionaries practice problem #2: default()

Screen Link:
https://app.dataquest.io/m/1020/dictionaries/2/default

My Code:

some_d = {0:"I", 1:"love"}

def default(some_dict, p_key, obj):
    for key, value in some_dict.items():
        if p_key in some_dict:
            return value
        else:
            return obj  
        
print(default(some_d, 2, "Python"))
print(default(some_d, 0, "You"))

What I expected to happen:
When I tested the code, the output matched the expectations of the practice problem:
Python
I

The error message below didn’t really clarify the exact error.

What actually happened:

Function default did not return the expected value.

I used ‘get a hint’ to see that I should try to use dict.get() so I’m attempting this now – just looking for a bit of discussion around why my code doesn’t work! Thanks.

2 Likes

Hey @colleen.mccaskell,
Internally gradle checks your solution with multiple inputs to make sure code is right.

If you try

print(default(some_d, 1, "You"))
I

Your code returns "I" instead of "love" So definitely your code is wrong some ware. Let me know if you need more help or you can try it by yourself.

2 Likes

The issue is related to how you have structured your approach above.

  • You loop through the dictionary
  • For each iteration, you check if p_key is in the dictionary or not
  • If it is you return the value corresponding to key.

So, first of all, you are looping through the dictionary for no reason. You can already check if p_key is in the dictionary or not through that if statement.

Secondly, you return value which corresponds to key and not p_key.

Taking the same dictionary as in the content -

some_d = {0:"I", 1:"love"}

And you run

print(default(some_d, 1, "Python"))

Note that we are trying to find the value for the key corresponding to 1 here.

That value would be love.

However, because of your current code, you will iterate over the dictionary. Your first iteration would have the key, value pair as 0, "I".

You then check if p_key is in some_dict or not. It is, we know that.

Then you return the value. Which is I. But the correct value corresponding to the p_key = 1 is love.

That’s the mistake in your code. It’s a simple enough fix and shouldn’t take you much time.

However, the more important point here is to focus on - how you test your code. Focus on what inputs you are trying to test your code on and if you are covering enough possible inputs. If you tested the above with the other key, 1 (as I show in the above example), you would have been more likely to notice the problem as well.

2 Likes

Thanks for this explanation – I managed to arrive at the correct answer when I switched to use the .get() method but I’m honestly not too sure how to amend the original code to get it working correctly…I’ve tried a few modifications but it just continues to return incorrect answers. Maybe I’m overcomplicating it!

1 Like

Since you got your answer through .get(), this is what the correct approach would be given your code in this post -

some_d = {0:"I", 1:"love"}

def default(some_dict, p_key, obj):
    if p_key in some_dict:
        return some_dict[p_key]
    else:
        return obj  
        
print(default(some_d, 2, "Python"))
print(default(some_d, 0, "You"))