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
Elizabeth McInerney
3,175 PointsPython Collections/Dictionaries/String formatting with dictionaries
I can't even make this work in workspaces. What am I doing wrong? (I did not copy over the dicts list, but imagine it is there)
string = "Hi, I'm {name} and I love to eat {food}!"
def string_factory(dicts,string):
count = 0
string_list = []
for item in dicts:
string_list[count] = string.format(**dicts[count])
count += 1
print(string_list[count])
string_factory(dicts,string)
4 Answers
vdivekar
3,544 PointsIt will really be easier and faster to troubleshoot if there's a direct link to the code challenge in question. Sometimes the code will not be "wrong" but it just won't give you what the question was asking. That's was the problem with the sillycase function. In this case, I don't know what dicts is supposed to be, or what the function's goal is. My guesses are that maybe there's a return missing like last time, or that line with the print should be indented once less than it currently is? You are also not using item, even though you wrote it to start the for-loop, so maybe you mean
string_list[count] = string.format(**item[count])
Also, if item is itself a dictionary, you don't have to write out [count].
Elizabeth McInerney
3,175 PointsMy other problem is that I cannot get returns to work in my workspaces code. So I just leave them out and use print statements to check that I got what I wanted. But then I forget to put the returns in when I transfer my code to the course.
Elizabeth McInerney
3,175 PointsI got it to work. Thanks so much for your help. Here is my code:
my_dicts = [
{'name': 'Michelangelo',
'food': 'PIZZA'},
{'name': 'Garfield',
'food': 'lasanga'},
{'name': 'Walter',
'food': 'pancakes'},
{'name': 'Galactus',
'food': 'worlds'}
]
my_string = "Hi, I'm {name} and I love to eat {food}!"
def string_factory(my_dicts,my_string):
count = 0
string_list = []
for item in my_dicts:
string_list.append(my_string.format(**my_dicts[count]))
count += 1
print(string_list)
return string_list
string_factory(my_dicts,my_string)
Elizabeth McInerney
3,175 PointsSorry about that, I thought I added they symbols for formatting. I will try again:
my_dicts = [
{'name': 'Michelangelo',
'food': 'PIZZA'},
{'name': 'Garfield',
'food': 'lasanga'},
{'name': 'Walter',
'food': 'pancakes'},
{'name': 'Galactus',
'food': 'worlds'}
]
my_string = "Hi, I'm {name} and I love to eat {food}!"
def string_factory(my_dicts,my_string):
count = 0
string_list = []
for item in my_dicts:
string_list.append(my_string.format(**my_dicts[count]))
count += 1
print(string_list)
return string_list
string_factory(my_dicts,my_string)
vdivekar
3,544 PointsNo problem, I was writing up a few things about your first reply but it looks like you beat me back here :). Couple things:
- When you write the language you want to post code in, it's all lowercase.
- Are you trying to use
returnin the workspace or the console? - If you're going with using
item, then you don't needcount, because you can use the append() method. - if you're going to use
count, then don't useitem. Your loop can begin withfor count in range(len(dicts))
Last two points are more to do with helping your code be easier to run, and not so much about making it correct.
Elizabeth McInerney
3,175 PointsAh, thanks for the lowercase tip, yes, I probably did that wrong. I have got return working in both places now. I guess I just prefer print in the workspace because I am usually trying to figure out what I did wrong, then I forget to switch to return when I move my code to the console. Thanks for the for count tip, I see how that would work. Yes, I see that I need to compress my code. It is working though.
Elizabeth McInerney
3,175 PointsElizabeth McInerney
3,175 PointsSorry, here is the entire code with the dictionary:
Elizabeth McInerney
3,175 PointsElizabeth McInerney
3,175 PointsNo, I am just using item to loop through dicts, which is a list of dictionaries. So dicts[0] is the first dictionary, dicts[1] is the second. One by one, each dictionary is used to fill out the string.