Test your code
Check normal examples, boundaries, and invalid input.
A test sets up an input, calls the code, and checks the result. Start with a case whose answer you can calculate yourself. Add edge cases, such as empty input and the exact point where a condition switches branches.
Use assert for the checks in this playground. The standard library’s unittest module can collect named tests in a larger project. Use a test assertion that compares the result to a known answer, and use explicit validation in application code for invalid input.
Choose a value outside the accepted range.
Try it yourself
Add a test for the upper boundary using value 12 and a test exactly on the upper boundary using value 10. Print the updated count when all four checks pass.
Target output
4 checks passedShow a hint
Both of the new calls should return 10.
Ready
Run your code to see the output here.
The first run downloads Python. Code runs in your browser. Progress and drafts are stored on this device.
Show one solution
def clamp(value, low, high):
return max(low, min(value, high))
assert clamp(-3, 0, 10) == 0
assert clamp(4, 0, 10) == 4
assert clamp(12, 0, 10) == 10
assert clamp(10, 0, 10) == 10
print("4 checks passed")