Editor

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 lesson

Write a function called validate_score(score) that:

  • Raises a TypeError with message "Score must be a number" if score is not an int or float
  • Raises a ValueError with message "Score must be between 0 and 100" if score is less than 0 or greater than 100
  • Returns the string "Valid score: X" where X is the score, if the input is valid
  • Then 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

    All Python exercises · 50 practice problems · Error Handling quiz