admin08077 commited on
Commit
524befa
verified
1 Parent(s): 269f35b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -17
app.py CHANGED
@@ -15,52 +15,108 @@ HEBREW_LETTERS = [
15
  ("砖", "Shin"), ("转", "Tav")
16
  ]
17
 
18
- # Initialize session state to keep track of:
19
- # 1) The randomized order of letters
20
  # 2) The index of the current letter
21
- # 3) Whether the user has guessed
 
 
 
 
22
  if "random_letters" not in st.session_state:
23
  st.session_state.random_letters = random.sample(HEBREW_LETTERS, len(HEBREW_LETTERS))
24
  if "current_index" not in st.session_state:
25
  st.session_state.current_index = 0
 
 
26
  if "answer_submitted" not in st.session_state:
27
  st.session_state.answer_submitted = False
28
  if "feedback" not in st.session_state:
29
  st.session_state.feedback = ""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
 
31
  def check_answer():
32
- user_guess = st.session_state.user_guess.strip().lower()
33
- correct_name = st.session_state.random_letters[st.session_state.current_index][1].lower()
 
 
 
34
 
35
- if user_guess == correct_name:
36
  st.session_state.feedback = "Correct! You鈥檙e crushing it, unstoppable problem-solver!"
 
37
  else:
38
- st.session_state.feedback = f"Wrong! The correct answer is '{correct_name.capitalize()}'. "\
39
- "Don鈥檛 sweat it鈥攔eal champions learn fast."
 
 
40
  st.session_state.answer_submitted = True
41
 
42
  def next_letter():
43
- st.session_state.answer_submitted = False
44
- st.session_state.user_guess = ""
45
- st.session_state.feedback = ""
46
-
47
  if st.session_state.current_index < len(st.session_state.random_letters) - 1:
48
  st.session_state.current_index += 1
49
  else:
50
  st.session_state.current_index = 0
51
  st.session_state.random_letters = random.sample(HEBREW_LETTERS, len(HEBREW_LETTERS))
 
52
 
53
- # App title
54
  st.title("Hebrew Alphabet Tester鈥擭o Nonsense, All Mastery")
55
 
 
 
 
 
56
  # Display the current letter
57
  current_letter, current_name = st.session_state.random_letters[st.session_state.current_index]
58
  st.markdown(f"### Letter: {current_letter}")
59
 
60
- # User input
61
  if not st.session_state.answer_submitted:
62
- st.text_input("What's this letter's name?", key="user_guess")
63
- st.button("Check Answer", on_click=check_answer)
 
 
 
 
 
 
64
  else:
 
65
  st.write(st.session_state.feedback)
66
- st.button("Next Letter", on_click=next_letter)
 
 
 
 
15
  ("砖", "Shin"), ("转", "Tav")
16
  ]
17
 
18
+ # --- Session State Variables ---
19
+ # 1) A shuffled list of all letters for testing
20
  # 2) The index of the current letter
21
+ # 3) The user鈥檚 multiple choice guess (A, B, C, or D)
22
+ # 4) The list of four choices for the current question
23
+ # 5) Whether the user has submitted the answer
24
+ # 6) Feedback message to show if correct or wrong
25
+ # 7) Track score (optional, if you want to show correct count)
26
  if "random_letters" not in st.session_state:
27
  st.session_state.random_letters = random.sample(HEBREW_LETTERS, len(HEBREW_LETTERS))
28
  if "current_index" not in st.session_state:
29
  st.session_state.current_index = 0
30
+ if "choices" not in st.session_state:
31
+ st.session_state.choices = []
32
  if "answer_submitted" not in st.session_state:
33
  st.session_state.answer_submitted = False
34
  if "feedback" not in st.session_state:
35
  st.session_state.feedback = ""
36
+ if "selected_option" not in st.session_state:
37
+ st.session_state.selected_option = None
38
+ if "score" not in st.session_state:
39
+ st.session_state.score = 0
40
+
41
+ # --- Helper Functions ---
42
+ def generate_choices(correct_name, all_letters):
43
+ """
44
+ Returns a list of exactly 4 possible answers (one correct + three random incorrect),
45
+ then shuffles them.
46
+ """
47
+ incorrect_names = [name for (letter, name) in all_letters if name != correct_name]
48
+ # Pick three random wrong answers
49
+ three_wrong = random.sample(incorrect_names, 3)
50
+ all_options = [correct_name] + three_wrong
51
+ random.shuffle(all_options)
52
+ return all_options
53
+
54
+ def load_new_question():
55
+ """
56
+ Loads the next question by resetting the answer submission state,
57
+ generating new multiple-choice options, and clearing feedback.
58
+ """
59
+ st.session_state.answer_submitted = False
60
+ st.session_state.feedback = ""
61
+ st.session_state.selected_option = None
62
+
63
+ current_letter, current_name = st.session_state.random_letters[st.session_state.current_index]
64
+ st.session_state.choices = generate_choices(current_name, HEBREW_LETTERS)
65
 
66
  def check_answer():
67
+ """
68
+ Checks if the selected_option is the correct name of the current letter.
69
+ Updates feedback and optionally increments score.
70
+ """
71
+ current_letter, current_name = st.session_state.random_letters[st.session_state.current_index]
72
 
73
+ if st.session_state.selected_option == current_name:
74
  st.session_state.feedback = "Correct! You鈥檙e crushing it, unstoppable problem-solver!"
75
+ st.session_state.score += 1
76
  else:
77
+ st.session_state.feedback = (
78
+ f"Wrong! The correct answer is '{current_name}'. "
79
+ "Don鈥檛 sweat it鈥攔eal champions learn fast."
80
+ )
81
  st.session_state.answer_submitted = True
82
 
83
  def next_letter():
84
+ """
85
+ Moves to the next letter, or reshuffles if we've reached the end.
86
+ Calls load_new_question() to generate new MC options.
87
+ """
88
  if st.session_state.current_index < len(st.session_state.random_letters) - 1:
89
  st.session_state.current_index += 1
90
  else:
91
  st.session_state.current_index = 0
92
  st.session_state.random_letters = random.sample(HEBREW_LETTERS, len(HEBREW_LETTERS))
93
+ load_new_question()
94
 
95
+ # --- Main App Execution ---
96
  st.title("Hebrew Alphabet Tester鈥擭o Nonsense, All Mastery")
97
 
98
+ # Generate choices once on first run or when next_letter is called
99
+ if not st.session_state.choices:
100
+ load_new_question()
101
+
102
  # Display the current letter
103
  current_letter, current_name = st.session_state.random_letters[st.session_state.current_index]
104
  st.markdown(f"### Letter: {current_letter}")
105
 
106
+ # Multiple-choice options
107
  if not st.session_state.answer_submitted:
108
+ # Radio button for the 4 choices (A, B, C, D)
109
+ st.session_state.selected_option = st.radio(
110
+ "Select the correct name:",
111
+ st.session_state.choices,
112
+ index=0
113
+ )
114
+ if st.button("Check Answer"):
115
+ check_answer()
116
  else:
117
+ # Show feedback after submission
118
  st.write(st.session_state.feedback)
119
+ # Optional: display current score
120
+ st.write(f"Score: {st.session_state.score}/{st.session_state.current_index + 1}")
121
+ if st.button("Next Letter"):
122
+ next_letter()