Bird
0
0

Identify the error in this Flask route for checking task status:

medium📝 Troubleshoot Q14 of 15
Flask - Background Tasks
Identify the error in this Flask route for checking task status:
@app.route('/status/<task_id>')
def status(task_id):
    state = tasks[task_id]
    return {'task': task_id, 'status': state}
AUsing tasks[task_id] without checking if key exists
BReturning a dictionary instead of JSON response
CMissing @app.route decorator
DIncorrect route URL syntax
Step-by-Step Solution
Solution:
  1. Step 1: Analyze dictionary access

    Accessing tasks[task_id] without checking key causes KeyError if missing.
  2. Step 2: Confirm other parts are correct

    Returning dict is allowed by Flask; decorator and route syntax are correct.
  3. Final Answer:

    Using tasks[task_id] without checking if key exists -> Option A
  4. Quick Check:

    Direct dict access risks KeyError [OK]
Quick Trick: Always check dict keys before access to avoid errors [OK]
Common Mistakes:
MISTAKES
  • Thinking returning dict is wrong in Flask
  • Ignoring possible KeyError on missing keys
  • Assuming route decorator is missing

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Flask Quizzes