0
0
Rest APIprogramming~10 mins

Validation error details in Rest API - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
A"Age cannot be negative"
BAge cannot be negative
CValueError
DNone
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a variable without quotes causes a NameError.
Not raising an error when age is negative.
2fill in blank
medium

Complete 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'
AValueError
BTypeError
CException
DNameError
Attempts:
3 left
💡 Hint
Common Mistakes
Catching the wrong exception type.
Using a generic Exception instead of ValueError.
3fill in blank
hard

Fix 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'
ANegativeAgeError
BNone
C"Age must be positive"
DAge must be positive
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a bare word without quotes causes a NameError.
Not providing any message at all.
4fill in blank
hard

Fill 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'
A>
B<
C10
D5
Attempts:
3 left
💡 Hint
Common Mistakes
Using < instead of > causes wrong filtering.
Using the wrong number for length comparison.
5fill in blank
hard

Fill 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'
Astartswith
Bin
C>
Dendswith
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'endswith' instead of 'startswith'.
Using 'not in' instead of 'in'.
Using wrong comparison operators.