Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Collections (2016, retired 2019) Dictionaries String Formatting with Dictionaries

Kushagra Patel
Kushagra Patel
7,740 Points

Challenge: help needed

Hi there , I cannot understand the working of this challenge, I have created a function that works on my local machine but not able to work online. Pls help: am I correct with my approach?

string_factory.py
def favorite_food(name=None,food=None):
    print("Hi, I'm {} and I love to eat {}!".format(name,food))

favorite_food(**{'name':'Michelangelo','food':'cake'})

2 Answers

Alex Koumparos
seal-mask
.a{fill-rule:evenodd;}techdegree
Alex Koumparos
Python Development Techdegree Student 36,887 Points

Hi Kushagra,

The challenge doesn't want you to change any of the function except the contents of the parentheses for the format method. Think about what code you can write just inside those parentheses that will cause the function, when called, to print "Hi, I'm <whatever name gets supplied as argument> and I love to eat <whatever food gets supplied as argument>!". Hint: you only need to enter six characters.

It also doesn't want you to call the function yourself.

Hope that points you in the right direction,

Cheers,

Alex

Kushagra Patel
Kushagra Patel
7,740 Points

Thank you so much for your help.

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 28,720 Points

You are understanding the concept of kwargs (keyword arguments), that is good. However, the challenge is looking for a solution that unpacks a dictionary-- much simpler than your solution. They were expecting you to modify the given code.

The named arguments are indeed significant.

See below:

def favorite_food(dict):
    print("Hi, I'm {name} and I love to eat {food}!".format(name=dict['name'], food=dict['food']))
Kushagra Patel
Kushagra Patel
7,740 Points

Thank you so much for your help