Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to import the Flask class.
Flask
from flask import [1]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Importing request instead of Flask
Using jsonify which is for JSON responses
Importing render_template which is for HTML templates
✗ Incorrect
The Flask class is imported from the flask module to create the app instance.
2fill in blank
mediumComplete the code to define a route that handles GET requests at '/' path.
Flask
@app.route('[1]', methods=['GET']) def home(): return 'Hello World!'
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '/home' or '/index' which are valid but not root
Forgetting the leading slash
✗ Incorrect
The root path '/' is used to define the main page route.
3fill in blank
hardFix the error in the code to catch exceptions in the route.
Flask
from flask import Flask app = Flask(__name__) @app.route('/data') def data(): try: result = 10 / [1] return str(result) except ZeroDivisionError: return 'Cannot divide by zero', 400
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 causes a ZeroDivisionError
Using None or empty string causes TypeError
✗ Incorrect
Dividing by 5 is valid and won't raise an exception. Dividing by 0 causes ZeroDivisionError.
4fill in blank
hardFill both blanks to raise and catch a ValueError in the route.
Flask
@app.route('/check') def check(): try: if int('abc') [1] ValueError: raise ValueError('Invalid number') except [2] as e: return str(e), 400
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' instead of '!=' in condition
Catching wrong exception type like TypeError
✗ Incorrect
The code checks if conversion raises ValueError and catches it properly.
5fill in blank
hardFill all three blanks to handle a KeyError in a route and return JSON error message.
Flask
from flask import jsonify @app.route('/item') def item(): data = {'name': 'apple'} try: value = data[[1]] return jsonify({{'value': value}}) except [2] as e: return jsonify({{'error': str(e)}}), [3]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using existing key 'name' so no error occurs
Catching wrong exception type
Returning wrong status code
✗ Incorrect
Accessing missing key 'price' raises KeyError, caught and returns 404 with JSON error.