Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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
220,513 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).