0
0
Flaskframework~10 mins

Accessing query parameters in Flask - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
A'query'
B'user'
C'id'
D'name'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong key string inside get(), like 'user' or 'id'.
Forgetting to use quotes around the key.
2fill in blank
medium

Complete 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'
ANone
B''
C'Guest'
D'User'
Attempts:
3 left
💡 Hint
Common Mistakes
Using None which returns None instead of a string.
Forgetting quotes around the default string.
3fill in blank
hard

Fix 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'
A'age'
Bage
C'Age'
D'AGE'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name without quotes.
Using wrong case like 'Age' or 'AGE'.
4fill in blank
hard

Fill 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'
Akey
Bvalue
Crequest.args[key]
Dfor
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'value' as loop variable instead of 'key'.
Forgetting to use request.args[key] to get the value.
5fill in blank
hard

Fill 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'
Akey
Brequest.args[key]
Cvalue
Drequest.args
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'value' as loop variable but not defining it.
Not accessing the value correctly with request.args[key].