Problem No 17
Create a function with keyword-only parameters
Medium≈ 15 minute session
Lesson guide
What this Python exercise practices
Create a function with keyword-only parameters is a intermediate practice lesson that focuses on functions, parameters, return values. It is designed to be solved in about 15 minutes with examples, starter code, and test feedback.
Prerequisites
- Python variables
- Function parameters
- Return values
Difficulty and time
- Level
- Intermediate
- Estimated time
- 15 minutes
Practice path
Summary
Practice defining keyword-only parameters using '*' and using them for clearer function calls.
Problem statement
Define format_point(x, y, *, label=None, precision=2) which returns a formatted string for the point (x, y). label and precision must be keyword-only parameters. precision controls the number of decimal places (use Python rounding rules with format specification). If label is provided, prepend it followed by ': '. For example, format_point(1.234, 4.567, label='P', precision=1) -> "P: (1.2, 4.6)".
Task
Write a function that enforces keyword-only arguments and uses them to control output formatting.
Examples
Labeled point with precision 1
Input
format_point(1.234, 4.567, label='A', precision=1)
Output
A: (1.2, 4.6)
Explanation
label is provided and precision=1 rounds coordinates to one decimal place.
Input format
A function call format_point(x, y, *, label=None, precision=2). label and precision must be passed as keywords if used.
Output format
Return a string representing the point, optionally prefixed with the label.
Constraints
Enforce label and precision as keyword-only by using '*' in the function signature. precision is a non-negative integer.
Samples
Sample input 0
format_point(1.234, 4.567)
Sample output 0
(1.23, 4.57)
Explanation 0
Default precision is 2 and no label is provided.
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 per unlocked preview lesson.