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

Error: string_factory() missing 1 required positional argument: 'template', but works in IDE

Heres my code, I keep getting the error mentioned in the title:

dicts = [ {'name': 'Michelangelo', 'food': 'PIZZA'}, {'name': 'Garfield', 'food': 'lasanga'}, {'name': 'Walter', 'food': 'pancakes'}, {'name': 'Galactus', 'food': 'worlds'} ]

template = "Hi, I'm {name} and I love to eat {food}!"

def string_factory(dicts, template): strings = [] for items in dicts: strings.append(template.format(**items)) return strings

string_factory(dicts, template)

Parth Try posting in markdown, the instructions are in the hyperlink below the posting window. It will look like this.

# Heres my code, I keep getting this error message
# Error: string_factory() missing 1 required positional argument: 'template', but works in IDE

dicts = [ {'name': 'Michelangelo', 'food': 'PIZZA'}, 
{'name': 'Garfield', 'food': 'lasanga'}, 
{'name': 'Walter', 'food': 'pancakes'},
 {'name': 'Galactus', 'food': 'worlds'} ]

template = "Hi, I'm {name} and I love to eat {food}!"

def string_factory(dicts, template): 
strings = [] 
for items in dicts: 
strings.append(template.format(**items)) 
return strings

string_factory(dicts, template)

template is a global variable established outside of the loop, I don't think you have to pass it to the routine, however I'm not sure that is what the issue is

1 Answer

Steven Parker
Steven Parker
231,248 Points

You should only take one argument.

The instructions say to write a function "that accepts a list of dictionaries as an argument". But your function requires a list of dictionaries and a template. You'll make use of the global template in your function, you won't get one as an argument.

And just define your function. Don't also call it (or define your own list of dicts).