Input Validator with raise
Intermediate · Write the code · from Python Try-Except: Handle Errors Without Crashing
From the lesson: Python Try-Except: Handle Errors Without Crashing
Imagine a trapeze artist performing high above the ground. No matter how skilled they are, there's always a safety net below. If they slip, the net catches them and they can climb back up and try again.
Read the lessonWrite a function called validate_score(score) that:
TypeError with message "Score must be a number" if score is not an int or floatValueError with message "Score must be between 0 and 100" if score is less than 0 or greater than 100"Valid score: X" where X is the score, if the input is validThen test it by calling the function with 85, "high", and 150, catching each error and printing the error message.
# Write your validate_score function here
# Test with 85, "high", and 150
More Try-Except exercises
- Safe DivisionBeginner
- Predict the OutputBeginner
- Fix the Number ConverterIntermediate
- Safe Dictionary LookupAdvanced
- Build a Safe CalculatorAdvanced
All Python exercises · 50 practice problems · Error Handling quiz