State management keeps an application's changing data available as users move through interactions. In a quiz app, the state records the current question, answer, evaluation, and the game's next step. section 9.1.1 section 9.4
A practical stateful design for the quiz app:
- Store the Game instance in Streamlit's session state so the same instance survives reruns. section 9.4
- Track status explicitly, beginning with GET_QUESTION and advancing through question display, answer evaluation, and the result. section 9.4
- Keep curr_question, curr_answer, and curr_eval inside Game instead of passing these values between unrelated functions. section 9.4
- Branch the frontend on the current status, then rerun after obtaining a question, submitting an answer, or evaluating it. section 9.4
Fact Frenzy quiz flow
The app obtains an LLM-generated question, displays it for an answer, evaluates the submitted response, and reports whether it is correct. Persistent storage, user accounts, and category selection remain outside the design. section 9.1.1 section 9.4
Listing 9.7 Moving through game states in a modified game.py
from llm import Llm
from prompts import QUESTION_PROMPT, ANSWER_PROMPT
from answer_evaluation import AnswerEvaluation
class Game:
def __init__(self, llm_api_key):
self.llm = Llm(llm_api_key)
self.status = 'GET_QUESTION'
self.curr_question = None
self.curr_answer = None
self.curr_eval = None
def ask_llm_for_question(self):
usr_msg, sys_msg = QUESTION_PROMPT['user'], QUESTION_PROMPT['system']
return self.llm.ask(usr_msg, sys_msg)
def ask_llm_to_evaluate_answer(self):
sys_msg = ANSWER_PROMPT['system']
user_msg = (
ANSWER_PROMPT['user']
.replace('{question}', self.curr_question)
.replace('{answer}', self.curr_answer)
)
reply = self.llm.ask(user_msg, sys_msg, AnswerEvaluation)
return reply
def obtain_question(self):
self.curr_question = self.ask_llm_for_question()
self.status = 'ASK_QUESTION'
return self.curr_question
def accept_answer(self, answer):
self.curr_answer = answer
self.status = 'EVALUATE_ANSWER'
def evaluate_answer(self):
self.curr_eval = self.ask_llm_to_evaluate_answer()
self.status = 'STATE_RESULT'
Inspect the Game implementation to see status initialization, current-value attributes, and methods that advance the quiz state. section 9.4








