Challenge - 5 Problems
Flask Parameter Converter Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output when accessing a route with converter?
Consider this Flask route:
What will be the response when visiting
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}"
Attempts:
2 left
💡 Hint
The converter ensures the parameter is an integer.
✗ Incorrect
The converter converts the URL part to an integer before passing it to the function. So id is the integer 42, and the returned string is 'Item ID is 42'.
📝 Syntax
intermediate2: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?Attempts:
2 left
💡 Hint
The converter syntax is <converter:name>.
✗ Incorrect
The correct syntax is . Option D reverses the order, C uses an invalid name, and D has no converter so price is a string.
🔧 Debug
advanced2:30remaining
Why does this route cause a 404 error for a path with slashes?
Given this Flask route:
What happens when you visit
@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}"
Attempts:
2 left
💡 Hint
The 'path' converter allows slashes in the parameter.
✗ Incorrect
The 'path' converter captures the entire remaining path including slashes. Without it, Flask treats the parameter as a string stopping at the first slash, so '/files/docs/manual.pdf' won't match and returns 404.
❓ state_output
advanced2:00remaining
What is the type and value of the parameter inside the view function?
Examine this Flask route:
What will be the output when visiting
@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__}"
Attempts:
2 left
💡 Hint
The converter converts the parameter to a float type.
✗ Incorrect
The converter converts the URL part to a float before passing it to the function. So cost is the float 19.99, and the type is float.
🧠 Conceptual
expert3: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?Attempts:
2 left
💡 Hint
Think about how URLs with slashes are matched by Flask converters.
✗ Incorrect
The converter captures the entire remaining path including slashes, allowing nested paths. The default converter stops at the first slash, so it cannot capture nested paths.