Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to raise a ValueError with a custom message.
Python
if age < 0: raise ValueError([1])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting quotes around the message
Using a variable name instead of a string
Trying to print instead of raise
✗ Incorrect
The raise statement needs a string message inside quotes to show a custom error message.
2fill in blank
mediumComplete the code to raise a TypeError with a custom message when input is not a string.
Python
if not isinstance(name, str): raise TypeError([1])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Not using quotes for the message
Raising the wrong error type
Using print instead of raise
✗ Incorrect
The error message must be a string inside quotes to explain the type error.
3fill in blank
hardFix the error in raising a custom error message for zero division.
Python
if denominator == 0: raise ZeroDivisionError([1])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the error class instead of a message
Using print instead of raise
Passing a number instead of a string
✗ Incorrect
The message must be a string explaining the error, inside quotes.
4fill in blank
hardFill both blanks to raise a custom error with a message including the invalid value.
Python
if score < 0 or score > 100: raise ValueError(f"Invalid score: [1]") # Use the variable name in the message invalid_value = [2]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong variable name
Not using the variable inside the f-string
Assigning invalid_value to a non-existent variable
✗ Incorrect
The variable 'score' holds the invalid value and should be used in the message and assigned to invalid_value.
5fill in blank
hardFill all three blanks to raise a custom error with a message showing the invalid input and its type.
Python
if not isinstance(data, dict): raise TypeError(f"Expected dict but got [1] of type [2]") actual_type = type([3]).__name__
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names in blanks
Not using f-string formatting
Forgetting to get the type name with __name__
✗ Incorrect
The variable 'data' is used to show the invalid input and get its type name.