Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Clamp Number to Range

Constrain a value to lie within a specified inclusive range [low, high].

Python practice8 minControl Flow (If–Else Logic)BeginnerLast updated March 15, 2026

Problem statement

Write a function clamp(value, low, high) that returns value constrained to the inclusive interval [low, high]. If value is less than low, return low. If value is greater than high, return high. Otherwise return value. You may assume low <= high.

Task

Implement a function clamp(value, low, high) that returns value adjusted to the inclusive bounds low and high.

Examples

Value inside range

Input

clamp(5, 1, 10)

Output

5

5 is between 1 and 10, so clamp returns 5.

Input format

Three numbers provided as function arguments: clamp(value, low, high). Assume low <= high.

Output format

Return a number (int or float) that lies within [low, high].

Constraints

Numbers are ints or floats. low will be less than or equal to high. Do not use external libraries.

Samples

Sample 1

Input

clamp(-3, 0, 7)

Output

0

Value -3 is below low (0), so return 0.