0
0
Flaskframework~10 mins

Parameter type converters (int, float, path) 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 an integer parameter named <user_id>.

Flask
from flask import Flask
app = Flask(__name__)

@app.route('/user/[1]')
def show_user(user_id):
    return f"User ID is {user_id}"
Drag options to blanks, or click blank then click option'
A<int:user_id>
B<string:user_id>
C<float:user_id>
D<path:user_id>
Attempts:
3 left
💡 Hint
Common Mistakes
Using will treat the parameter as text, not an integer.
Forgetting the converter and just using treats it as a string.
2fill in blank
medium

Complete the code to define a route that accepts a floating-point number parameter named <price>.

Flask
from flask import Flask
app = Flask(__name__)

@app.route('/price/[1]')
def show_price(price):
    return f"Price is {price}"
Drag options to blanks, or click blank then click option'
A<path:price>
B<int:price>
C<float:price>
D<string:price>
Attempts:
3 left
💡 Hint
Common Mistakes
Using will cause errors if the URL has decimals.
Using treats the parameter as text.
3fill in blank
hard

Fix the error in the route to accept a file path parameter named <filepath> that can include slashes.

Flask
from flask import Flask
app = Flask(__name__)

@app.route('/files/[1]')
def show_file(filepath):
    return f"File path is {filepath}"
Drag options to blanks, or click blank then click option'
A<path:filepath>
B<int:filepath>
C<string:filepath>
D<float:filepath>
Attempts:
3 left
💡 Hint
Common Mistakes
Using stops at the first slash, so the path is incomplete.
Using or causes type errors.
4fill in blank
hard

Fill both blanks to create a route that accepts an integer <post_id> and a path <filename>.

Flask
from flask import Flask
app = Flask(__name__)

@app.route('/post/[1]/file/[2]')
def show_post_file(post_id, filename):
    return f"Post {post_id}, File {filename}"
Drag options to blanks, or click blank then click option'
A<int:post_id>
B<string:filename>
C<path:filename>
D<float:post_id>
Attempts:
3 left
💡 Hint
Common Mistakes
Using will not capture slashes in the filename.
Using is incorrect if the ID is an integer.
5fill in blank
hard

Fill all three blanks to create a route that accepts a float <rating>, an int <item_id>, and a path <image_path>.

Flask
from flask import Flask
app = Flask(__name__)

@app.route('/rate/[1]/item/[2]/image/[3]')
def show_rating(rating, item_id, image_path):
    return f"Rating: {rating}, Item: {item_id}, Image: {image_path}"
Drag options to blanks, or click blank then click option'
A<string:image_path>
B<int:item_id>
C<path:image_path>
D<float:rating>
Attempts:
3 left
💡 Hint
Common Mistakes
Using will not capture slashes.
Mixing up the order of converters causes errors.