0
0
Prompt Engineering / GenAIml~10 mins

Fallback and error handling in Prompt Engineering / GenAI - Interactive Code Practice

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

Complete the code to catch an error during model prediction.

Prompt Engineering / GenAI
try:
    prediction = model.predict(data)
except [1]:
    prediction = None
Drag options to blanks, or click blank then click option'
AValueError
BKeyError
CTypeError
DException
Attempts:
3 left
💡 Hint
Common Mistakes
Using a specific error type that might miss other errors.
2fill in blank
medium

Complete the code to provide a fallback prediction when the model fails.

Prompt Engineering / GenAI
try:
    result = model.predict(input_data)
except Exception:
    result = [1]
Drag options to blanks, or click blank then click option'
Amodel.predict(default_data)
BNone
Cinput_data
D[]
Attempts:
3 left
💡 Hint
Common Mistakes
Setting result to None which may cause errors later.
3fill in blank
hard

Fix the error in the code to log the error message correctly.

Prompt Engineering / GenAI
try:
    output = model.predict(data)
except Exception as [1]:
    print(f"Error: {e}")
Drag options to blanks, or click blank then click option'
Aerr
Be
Cexception
Derror
Attempts:
3 left
💡 Hint
Common Mistakes
Mismatch between error variable name and print statement.
4fill in blank
hard

Fill both blanks to retry prediction only if the error is a ValueError.

Prompt Engineering / GenAI
try:
    prediction = model.predict(data)
except [1] as e:
    if isinstance(e, [2]):
        prediction = model.predict(fallback_data)
Drag options to blanks, or click blank then click option'
AException
BValueError
CTypeError
DKeyError
Attempts:
3 left
💡 Hint
Common Mistakes
Catching only ValueError in except, missing other errors.
5fill in blank
hard

Fill all three blanks to log the error, retry prediction, and set fallback if retry fails.

Prompt Engineering / GenAI
try:
    prediction = model.predict(data)
except [1] as err:
    print(f"Error occurred: {err}")
    try:
        prediction = model.predict([2])
    except [3]:
        prediction = None
Drag options to blanks, or click blank then click option'
AException
Bfallback_data
DValueError
Attempts:
3 left
💡 Hint
Common Mistakes
Using different error types inconsistently.