Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to raise a ValueError with a message.
Rest API
if age < 0: raise ValueError([1])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a variable without quotes causes a NameError.
Not raising an error when age is negative.
✗ Incorrect
We must pass a string message to ValueError to explain the problem.
2fill in blank
mediumComplete the code to catch a ValueError exception.
Rest API
try: number = int(user_input) except [1]: print("Invalid number")
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Catching the wrong exception type.
Using a generic Exception instead of ValueError.
✗ Incorrect
ValueError is raised when int() fails to convert a string to an integer.
3fill in blank
hardFix the error in raising a custom exception with a message.
Rest API
class NegativeAgeError(Exception): pass if age < 0: raise NegativeAgeError([1])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a bare word without quotes causes a NameError.
Not providing any message at all.
✗ Incorrect
The message must be a string, so it needs quotes.
4fill in blank
hardFill both blanks to create a dictionary of errors with messages longer than 10 characters.
Rest API
errors = {field: msg for field, msg in error_dict.items() if len(msg) [1] [2] Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using < instead of > causes wrong filtering.
Using the wrong number for length comparison.
✗ Incorrect
We want messages longer than 10 characters, so use > and 10.
5fill in blank
hardFill all three blanks to filter errors with keys starting with 'user' and messages containing 'invalid'.
Rest API
filtered = {k: v for k, v in errors.items() if k.[1]('user') and 'invalid' [2] v and len(v) [3] 0} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'endswith' instead of 'startswith'.
Using 'not in' instead of 'in'.
Using wrong comparison operators.
✗ Incorrect
We check if key starts with 'user', 'invalid' is in the message, and message length is greater than 0.