Complete the code to import the Flask class.
from flask import [1]
The Flask class is imported from the flask module to create the app instance.
Complete the code to define a route that handles GET requests at '/' path.
@app.route('[1]', methods=['GET']) def home(): return 'Hello World!'
The root path '/' is used to define the main page route.
Fix the error in the code to catch exceptions in the route.
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
Dividing by 5 is valid and won't raise an exception. Dividing by 0 causes ZeroDivisionError.
Fill both blanks to raise and catch a ValueError in the route.
@app.route('/check') def check(): try: if int('abc') [1] ValueError: raise ValueError('Invalid number') except [2] as e: return str(e), 400
The code checks if conversion raises ValueError and catches it properly.
Fill all three blanks to handle a KeyError in a route and return JSON error message.
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]
Accessing missing key 'price' raises KeyError, caught and returns 404 with JSON error.
