Clamp
限制范围
The smallest useful function with multiple parameters. Every UI slider, audio mixer, and game has this.
The problem
Return value confined to [lo, hi]. Below
range → return lo. Above → return hi.
Inside → return value.
The pattern
Python · runnable
Early-return form (also fine)
return exits the function — no else needed
Check yourself
clamp(5, 0, 10) — what does this return?
Now you try
Open 01-clamp.py. 6 tests including both boundaries and a
negative-range case.