Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to get the value of the 'name' query parameter in a Flask route.
Flask
from flask import Flask, request app = Flask(__name__) @app.route('/hello') def hello(): name = request.args.get([1]) return f"Hello, {name}!"
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong key string inside get(), like 'user' or 'id'.
Forgetting to use quotes around the key.
✗ Incorrect
The query parameter key is 'name', so request.args.get('name') retrieves its value.
2fill in blank
mediumComplete the code to provide a default value 'Guest' if the 'name' query parameter is missing.
Flask
from flask import Flask, request app = Flask(__name__) @app.route('/welcome') def welcome(): name = request.args.get('name', [1]) return f"Welcome, {name}!"
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using None which returns None instead of a string.
Forgetting quotes around the default string.
✗ Incorrect
The second argument to get() is the default value returned if the parameter is missing. 'Guest' is the desired default.
3fill in blank
hardFix the error in the code to correctly access the 'age' query parameter as an integer.
Flask
from flask import Flask, request app = Flask(__name__) @app.route('/age') def age(): age = int(request.args.get([1], 0)) return f"Age is {age}"
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name without quotes.
Using wrong case like 'Age' or 'AGE'.
✗ Incorrect
The key must be the exact string 'age' matching the query parameter name. Using a variable or wrong case causes errors.
4fill in blank
hardFill all three blanks to create a dictionary comprehension that maps each query parameter key to its value.
Flask
from flask import Flask, request app = Flask(__name__) @app.route('/params') def params(): params_dict = { [1]: [2] for [3] in request.args } return str(params_dict)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'value' as loop variable instead of 'key'.
Forgetting to use request.args[key] to get the value.
✗ Incorrect
In a dictionary comprehension, the syntax is {key: value for key in iterable}. Here, key is 'key', value is request.args[key], and the loop variable is 'key'.
5fill in blank
hardFill all three blanks to filter query parameters with values longer than 3 characters into a new dictionary.
Flask
from flask import Flask, request app = Flask(__name__) @app.route('/filter') def filter_params(): filtered = { [1]: [2] for [3] in request.args if len(request.args[[3]]) > 3 } return str(filtered)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'value' as loop variable but not defining it.
Not accessing the value correctly with request.args[key].
✗ Incorrect
The dictionary comprehension uses 'key' as the loop variable, maps key to request.args[key], and filters by length of the value.