Challenge - 5 Problems
Session Storage Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this Flask session code?
Consider this Flask route that sets and reads session data. What will be printed when accessing the '/count' URL twice in a row?
Flask
from flask import Flask, session app = Flask(__name__) app.secret_key = 'secret' @app.route('/count') def count(): if 'visits' in session: session['visits'] += 1 else: session['visits'] = 1 return f"Visit count: {session['visits']}"
Attempts:
2 left
💡 Hint
Think about how Flask session stores data between requests for the same client.
✗ Incorrect
Flask session stores data in a cookie signed with the secret key. On first visit, 'visits' is set to 1. On second visit, it increments to 2.
📝 Syntax
intermediate1:30remaining
Which option correctly sets a session variable in Flask?
You want to store the username 'alice' in the Flask session. Which code snippet does this correctly?
Attempts:
2 left
💡 Hint
Flask session behaves like a dictionary.
✗ Incorrect
Flask session is a dictionary-like object. You set values using square brackets and assignment.
🔧 Debug
advanced2:00remaining
Why does this Flask session code raise a RuntimeError?
Examine this Flask code snippet. Why does it raise a RuntimeError: Working outside of request context?
Flask
from flask import session def set_user(): session['user'] = 'bob' set_user()
Attempts:
2 left
💡 Hint
When does Flask create the session object?
✗ Incorrect
Flask session is only available during a request. Calling session outside a request context causes RuntimeError.
❓ state_output
advanced2:00remaining
What is the session content after this sequence of requests?
Given this Flask route, what will be the session content after visiting '/add/5' then '/add/3'?
Flask
from flask import Flask, session app = Flask(__name__) app.secret_key = 'secret' @app.route('/add/<int:num>') def add(num): total = session.get('total', 0) total += num session['total'] = total return f"Total: {total}"
Attempts:
2 left
💡 Hint
Session data persists and accumulates across requests from the same client.
✗ Incorrect
First request sets total to 5, second adds 3 making total 8 in session.
🧠 Conceptual
expert2:30remaining
Which statement about Flask session storage is true?
Select the correct statement about how Flask session stores data by default.
Attempts:
2 left
💡 Hint
Think about where Flask keeps session data without extra setup.
✗ Incorrect
By default, Flask stores session data client-side in a cookie signed with the secret key to prevent tampering.