Python requires the function body to be indented. Here, print and return are not indented under def hello():.
Step 2: Verify decorator and function syntax
The decorator has correct parentheses and the function name is valid, so no errors there.
Final Answer:
The function body is not indented properly. -> Option D
Quick Check:
Python needs indented function bodies [OK]
Hint: Indent function code under def line [OK]
Common Mistakes:
Forgetting to indent function body
Removing parentheses from decorator
Misnaming the function
5. You want to create a Flask route that accepts a username in the URL like /profile/john and returns Hello, john!. Which code correctly achieves this?
hard
A. @app.route('/profile/<username>')\ndef profile():\n return f'Hello, {username}!'
B. @app.route('/profile/')\ndef profile(username):\n return f'Hello, {username}!'
C. @app.route('/profile/<username>')\ndef profile(username):\n return f'Hello, {username}!'
D. @app.route('/profile/username')\ndef profile():\n return 'Hello, username!'
Solution
Step 1: Use variable URL part for username
The route must capture username as a string variable using <username> in the URL.
Step 2: Define function with matching parameter and return greeting
The function must accept username as a parameter and return the greeting using an f-string.
Final Answer:
@app.route('/profile/<username>')\ndef profile(username):\n return f'Hello, {username}!' -> Option C