Python Python Collections Dictionaries String Formatting with Dictionaries

Travis John Villanueva
Travis John Villanueva
5,037 Points

It works fine in the workspace-dont know what is the problem

Hi I tested my code via workspace and im stuck with this problem. I dont know how to debug it as it is working fine? Do you think its a bug?

string_factory.py
d = dict(name="Travis", food="PIZZA")
def favorite_food(name,food):
    return "Hi, I'm {} and I love to eat {}!".format(name,food)
print(favorite_food(**d))

3 Answers

Steven Parker
Steven Parker
149,192 Points

Here's a few hints:

  • don't change the function definition. It should still take a single (dictionary) argument
  • you don't need to define anything before the function
  • you just need to provide values to the "format" by unpacking the dictionary
  • you won't need to "print" anything
Travis John Villanueva
Travis John Villanueva
5,037 Points

Hi Steven Parker, Thank you for the reply. I spend the whole day figuring out how to unpack a dictionary by creating a function then im going to call that inside the favorite_food function. Little did i know that the commentary above the problem serve as a clue.

To give a little more help if you still haven't got it:

  • Just as Steven Parker stated - "don't change the function definition."
    • But you will have to add to this function.
  • Steven Parker also stated - "you just need to provide values to the "format" by unpacking the dictionary"
    • Stop and think how to get a value from a keyword in a dictionary. Hint: test_dict["a"] - Example show to us by Kenneth Love, returns the value 3.
    • The keyword was ["a"] that held the value of 3, while test_dict is the name of the dictionary that holds the keyword and value.
  • Now, Travis John Villanueva stated - "Little did I know that the commentary above the problem serve as a clue."
    • The placeholders are a keyword, they just need a value assigned to them.

Thanks, Steven Parker and Travis John Villanueva for not giving the answer but helping us all stop and think to help with our learning. Never stop trying. It will come to you.

Steven Parker
Steven Parker
149,192 Points

Jason — if you start getting a lot of spam email, you might want to replace that live email link with a partly obfuscated reference so spam-bots won't recognize it.