easy +5 pts

Clamp a Number to a Range

Write a function that restricts a number to a given inclusive interval.

Write a function `clamp(value, lower, upper)` that returns: - `lower` if `value` is less than `lower`, - `upper` if `value` is greater than `upper`, - otherwise `value` itself. All inputs are numbers (int or float). The range is inclusive, and it is guaranteed that `lower <= upper`.

Constraints

Inputs can be any integer or floating-point numbers. The lower bound will always be less than or equal to the upper bound. No type checking is required.

Example

>>> clamp(5, 1, 10)
5
>>> clamp(-5, 1, 10)
1
>>> clamp(15, 1, 10)
10
>>> clamp(2.5, 0.0, 1.5)
1.5
5 points ~5 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Use simple if–elif–else logic to compare value with lower and upper.
The return value must be one of the three: value, lower, or upper.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.