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

Ben Thrower
Ben Thrower
1,760 Points

Output not a string on string_factory.py.

The response I keep getting is that the output from my function is not a string. Not sure why.

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

print(favorite_food(**{"name": "Ben", "food": "pizza"}))

1 Answer

Louise St. Germain
Louise St. Germain
19,424 Points

Hi Ben,

The main issue is that the challenge program is passing a dictionary to the favorite_food function, and the way you have it set up, it assigns the whole dictionary to the first variable (food) and the second variable remains None. Then strange things happen because it fails your if statement and the function doesn't return anything (hence, it's complaining that it's not getting a string back).

The solution is a lot simpler than what you have here, so your best bet is to restart the challenge and just go with the code they have there. They are giving you this:

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

The challenge program is passing a dictionary to the favorite_food function, and it's fine to leave it that way. So basically you just have to keep track of the fact that the variable dict contains a dictionary.

Similarly, there's no need to mess with the fact that it has {name} and {food} placeholders in the string. Those variables will exist when you unpack the dictionary. So all you have to do is unpack the dict variable inside the format() method, and everything should work.

I hope this helps!