Code Challenge
Accept a booking code containing BK- followed by exactly three digits. Reject added prefixes or suffixes.
Understand the idea
Write your own solution before opening the worked answer. Predict the output from the supplied values, then compare the actual result. Check both an ordinary case and the boundary described below; a passing example is more useful when you can explain why it passes.
Where to try it
Open this example with Try in playground. The code loads as editable text and does not run until you select Run. The first run downloads Python. Each run starts fresh; temporary files created by a script are not files on your PC.
Your task
Accept a booking code containing BK- followed by exactly three digits. Reject added prefixes or suffixes.
Show the worked solution
import re
for code in ["BK-123", "xBK-123", "BK-1234"]:
print(bool(re.fullmatch(r"BK-[0-9]{3}", code)))
Check the result
The results are True, False, and False.
Test one more case
Add blank and lower-case inputs and decide whether either should be allowed.