Command Palette

Search for a command to run...

Hectal
Phase 7: Errors, Files and Data Formats
Python · Exercise 2 of 7 PreviousNext
Intermediate

Raise your own error

Define class InvalidScore(ValueError): pass.

Write grade(score). If the score is below 0 or above 100, raise InvalidScore with a message that includes the score. Otherwise return the text "pass" for a score of 40 or more, and "fail" for a score below 40.

What Check tests

  1. grade(75) is pass
  2. grade(10) is fail
  3. grade(40) is pass (boundary)
  4. grade(120) raises InvalidScore
  5. grade(-1) raises a ValueError
  6. The boundaries 0, 39 and 100 are valid
  7. The message includes the bad score

Stuck? One hint at a time

The solution unlocks after your first Check. Try first: that is where the learning happens.

main.py
Sign in to run your code.

Output

Press Run to see what your program prints.