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 trialIdan shami
13,251 PointsI got lost in this challenge(string formatting with dictionaries)
I have no idea what to do, please help
# Example:
# values = [{"name": "Michelangelo", "food": "PIZZA"}, {"name": "Garfield", "food": "lasagna"}]
# string_factory(values)
# ["Hi, I'm Michelangelo and I love to eat PIZZA!", "Hi, I'm Garfield and I love to eat lasagna!"]
def string_factory([{name="Kenneth", food="tacos"}]):
template = "Hi, I'm {name} and I love to eat {food}!".format(name, food)
return template
1 Answer
Chris Freeman
Treehouse Moderator 68,441 PointsYou're not far off. The challenge asks the function to accept a list of dictionaries. add a loop to use each dictionary in the list to format a new string. Append this string to a new list that will be returned after the loop completes.
Post back if you need more help. Good luck!!
Idan shami
13,251 PointsIdan shami
13,251 PointsI still don't get it... they said "accepts a list of dictionaries as an argument" you said i didn't accept a list of dictionaries but i did... in the argument
def string_factory([{name="Kenneth", food="tacos"}]):
and I don't understand where I need to put two stars..... thanks, I hope you will help me again cause I still don't know what to do...
Chris Freeman
Treehouse Moderator 68,441 PointsChris Freeman
Treehouse Moderator 68,441 PointsIt is a syntax error to use a literal object, such as a list, as an parameter.
The proper syntax is to use a variable name. That variable name will get assigned a list of dicts by the calling function, which in this case would be the challenge checker.
The form you seek is:
Idan shami
13,251 PointsIdan shami
13,251 PointsAm I getting closer?
I don't understand how to append to list1, but tell me if I am right until now... thank you !! i don't know what would I done without you... good day.
Chris Freeman
Treehouse Moderator 68,441 PointsChris Freeman
Treehouse Moderator 68,441 PointsGetting closer, but need to fix:
template
is a string which has noappend()
method. Instead append the template tolist1
:list1.append(template)
Other changes to improve the code
values
as the challenge checker will set its value during testingtemplate
misses the point of using the template. It would have been better to use something likenew_string = template.format(**v)
then append new_string to list1.
list1
should be initialized inside the function.I think you get it on this pass! Good luck!!
Idan shami
13,251 PointsIdan shami
13,251 Pointsit worked !!! thank you so much, I understand it better now! thank you so much!