Lesson 15 — Prompt templates as functions
Stop retyping good prompts
Once you have a prompt that works well, do not retype it every time — make it a function. The parts that change become parameters; the parts that stay the same are written once.
def explain_prompt(code, level="beginner"):
return f"Explain this Python code at {level} level, line by line:\n{code}"
A default parameter (level="beginner") is used when you do not pass a value, so the common case stays short and the special case is still possible: explain_prompt(code, level="expert").
Benefits: every prompt has the same good structure, you can improve the template in one place, and you can test it like any other function.
Why it matters: this is how prompts live inside real apps — as code, with parameters, reviewed and tested like the rest of the program.
Write fix_prompt(code, error, level="beginner") that returns the 3-line prompt shown in expected output. It is used twice: once with the default level, once with "expert".
Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.
A quick 3-question quiz on this lesson — instant feedback.
Ready for more? Optional paid resources that pick up where this lesson leaves off.
Some links on this page are affiliate links: we may earn a commission at no extra cost to you. As an Amazon Associate I earn from qualifying purchases.
Project-based Coding with AI: ChatGPT & Claude courses if you want a structured paid path after the free lessons. Wait for a sale — prices drop often.
Browse on Udemy →Search Amazon for Coding with AI: ChatGPT & Claude books — check the publication year so the examples match current versions.
Search Amazon →