Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to start the Flask app.
Flask
if __name__ == '__main__': app.[1]()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
start() or launch() which are not Flask methods.✗ Incorrect
The run() method starts the Flask application server.
2fill in blank
mediumComplete the code to define a route for task status.
Flask
@app.route('/status/[1]') def task_status(task_id): return f"Status of task {task_id}"
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different variable name in the route than in the function parameter.
✗ Incorrect
The route uses task_id as a variable part of the URL to identify the task.
3fill in blank
hardFix the error in the code to return JSON response with task status.
Flask
from flask import jsonify @app.route('/status/<task_id>') def task_status(task_id): status = {'task_id': task_id, 'status': 'running'} return [1](status)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
json which is a module, not a Flask response function.✗ Incorrect
The jsonify() function converts a Python dictionary to a JSON response in Flask.
4fill in blank
hardFill both blanks to check if a task is complete and return appropriate status.
Flask
def check_task(task_id): if tasks[task_id] [1] 'complete': return [2]({'task_id': task_id, 'status': 'done'}) else: return jsonify({'task_id': task_id, 'status': 'running'})
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
!= instead of == for checking completion.✗ Incorrect
Use == to compare status and jsonify to return JSON response.
5fill in blank
hardFill all three blanks to create a dictionary comprehension filtering completed tasks.
Flask
completed_tasks = {task_id: status for task_id, status in tasks.items() if status [1] 'complete' and task_id [2] [3] Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong comparison operators or wrong task id string.
✗ Incorrect
The comprehension filters tasks with status 'complete' and excludes 'task_1'.