0
0
Flaskframework~20 mins

Parameter type converters (int, float, path) in Flask - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Flask Parameter Converter Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when accessing a route with converter?
Consider this Flask route:
from flask import Flask
app = Flask(__name__)

@app.route('/item/<int:id>')
def item(id):
    return f"Item ID is {id}"

What will be the response when visiting /item/42 in the browser?
Flask
from flask import Flask
app = Flask(__name__)

@app.route('/item/<int:id>')
def item(id):
    return f"Item ID is {id}"
AItem ID is 42
BItem ID is '42'
C404 Not Found error
DTypeError because id is not an int
Attempts:
2 left
💡 Hint
The converter ensures the parameter is an integer.
📝 Syntax
intermediate
2:00remaining
Which route definition correctly uses the converter?
You want to create a Flask route that accepts a floating-point number as a URL parameter named price. Which of these route decorators is correct?
A@app.route('/cost/<price:float>')
B@app.route('/cost/<price>')
C@app.route('/cost/<float_price>')
D@app.route('/cost/<float:price>')
Attempts:
2 left
💡 Hint
The converter syntax is <converter:name>.
🔧 Debug
advanced
2:30remaining
Why does this route cause a 404 error for a path with slashes?
Given this Flask route:
@app.route('/files/<path:filename>')
def files(filename):
    return f"File requested: {filename}"

What happens when you visit /files/docs/manual.pdf? Why might a similar route without path: cause a 404 error?
Flask
@app.route('/files/<path:filename>')
def files(filename):
    return f"File requested: {filename}"
AThe route matches but filename is only 'manual.pdf'; 'path:' has no effect.
BThe route causes a 404 because 'path:' is invalid syntax in Flask routes.
CThe route matches and filename is 'docs/manual.pdf'; without 'path:' it would only match 'docs' causing 404.
DThe route matches but filename is a list of path parts, not a string.
Attempts:
2 left
💡 Hint
The 'path' converter allows slashes in the parameter.
state_output
advanced
2:00remaining
What is the type and value of the parameter inside the view function?
Examine this Flask route:
@app.route('/price/<float:cost>')
def show_price(cost):
    return f"Cost is {cost} and type is {type(cost).__name__}"

What will be the output when visiting /price/19.99?
Flask
@app.route('/price/<float:cost>')
def show_price(cost):
    return f"Cost is {cost} and type is {type(cost).__name__}"
ACost is '19.99' and type is str
BCost is 19.99 and type is float
CCost is 19 and type is int
DCost is 19.99 and type is str
Attempts:
2 left
💡 Hint
The converter converts the parameter to a float type.
🧠 Conceptual
expert
3:00remaining
Why use converter instead of for file paths?
In Flask routing, what is the main reason to use the <path:filename> converter instead of the default <string:filename> when accepting file paths as parameters?
A<path:> allows slashes in the parameter, so it can capture nested paths; <string:> stops at the first slash.
B<path:> automatically decodes URL-encoded characters; <string:> does not.
C<path:> converts the parameter to a list of strings; <string:> keeps it as a single string.
D<path:> is faster to process than <string:> for long strings.
Attempts:
2 left
💡 Hint
Think about how URLs with slashes are matched by Flask converters.