Lesson guide
What this Python exercise practices
Create a Multiplier Factory is a beginner practice lesson that focuses on functions, parameters, return values. It is designed to be solved in about 10 minutes with examples, starter code, and test feedback.
Prerequisites
- Python variables
- Function parameters
- Return values
Difficulty and time
- Level
- Beginner
- Estimated time
- 10 minutes
Practice path
Related public exercises
Summary
Build a closure that captures a multiplier and returns a function to multiply inputs by that value.
Problem statement
Write a function factory make_multiplier(m) that returns a new function. The returned function should accept a single numeric argument x and return x * m. Ensure the multiplier m is captured in the closure so that the returned function continues to use m even after make_multiplier returns. Avoid using global variables.
Task
Implement make_multiplier(m) which returns a function that multiplies its argument by m, reinforcing how closures preserve configuration values.
Examples
Basic usage
Input
make_multiplier(4)(3)
Output
12
Explanation
make_multiplier(4) returns a function that multiplies by 4. Calling it with 3 returns 3 * 4 = 12.
Input format
A single expression calling make_multiplier with a number and then calling the returned function with a numeric argument, e.g., make_multiplier(5)(3).
Output format
Return the numeric result of multiplying the argument by the captured multiplier. The test harness compares the string form of the return value.
Constraints
- Implement make_multiplier as a closure (inner function capturing m). - Do not use global variables. - Support integers (and floats) as inputs.
Samples
Sample input 0
make_multiplier(1)(999)
Sample output 0
999
Explanation 0
Multiplying by 1 returns the original value.
AI assistant
Ask me anything!
Need help? I can explain the core idea behind this problem, review your current code, and give targeted hints. Use “Teach Theory” for the concept, “Get AI hint” for a quick scaffold nudge, or ask a specific question below.
Chat history is temporary and will not be saved.
Free preview includes 1 Teach Theory response and 1 AI hint on each of the first 3 lessons in this module.