Python Python Collections Dictionaries String Formatting with Dictionaries

Travis John Villanueva
Travis John Villanueva
4,247 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))

1 Answer

Steven Parker
Steven Parker
143,254 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
4,247 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.