Complete worked example
python
def parse_limit(raw):
if type(raw) is int:
value = raw
elif isinstance(raw, str):
try:
value = int(raw)
except ValueError as error:
raise ValueError("Expected an integer") from error
else:
raise ValueError("Unsupported representation")
if not 1 <= value <= 3:
raise ValueError("Outside range")
return value- Reasoning
- Representation, conversion and inclusive range checks have separate responsibilities.