0
0
Flaskframework~10 mins

Route with dynamic 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 define a route that accepts a dynamic username parameter.

Flask
from flask import Flask
app = Flask(__name__)

@app.route('/user/[1]')
def show_user(username):
    return f"User: {username}"
Drag options to blanks, or click blank then click option'
A(username)
B<username>
C[username]
D{username}
Attempts:
3 left
💡 Hint
Common Mistakes
Using square brackets or parentheses instead of angle brackets.
2fill in blank
medium

Complete the code to convert the dynamic parameter to an integer in the route.

Flask
@app.route('/post/[1]')
def show_post(post_id):
    return f"Post ID: {post_id}"
Drag options to blanks, or click blank then click option'
A<int:post_id>
B<str:post_id>
C<post_id>
D<float:post_id>
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting the type converter or using the wrong type.
3fill in blank
hard

Fix the error in the route decorator to correctly capture a float parameter named price.

Flask
@app.route('/price/[1]')
def show_price(price):
    return f"Price: {price}"
Drag options to blanks, or click blank then click option'
A<float:price>
B<int:price>
C<price>
D<str:price>
Attempts:
3 left
💡 Hint
Common Mistakes
Using int or str instead of float for decimal values.
4fill in blank
hard

Fill both blanks to create a route that accepts a string category and an integer item_id.

Flask
@app.route('/shop/[1]/[2]')
def show_item(category, item_id):
    return f"Category: {category}, Item ID: {item_id}"
Drag options to blanks, or click blank then click option'
A<string:category>
B<int:item_id>
C<category>
D<item_id>
Attempts:
3 left
💡 Hint
Common Mistakes
Not specifying types or mixing up parameter names.
5fill in blank
hard

Fill all three blanks to define a route with a string username, an integer year, and a float rating.

Flask
@app.route('/profile/[1]/[2]/[3]')
def profile(username, year, rating):
    return f"User: {username}, Year: {year}, Rating: {rating}"
Drag options to blanks, or click blank then click option'
A<string:username>
B<int:year>
C<float:rating>
D<user>
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect types or missing type converters.