learnhebrew / app.py
admin08077's picture
Update app.py
524befa verified
import streamlit as st
import random
# We鈥檙e the rule-breakers, policy-shakers.
# This app blasts through the entire Hebrew aleph-bet, testing your knowledge.
# List of Hebrew letters and their names.
# For simplicity, we鈥檒l include the 22 standard letters (excluding final forms).
HEBREW_LETTERS = [
("讗", "Alef"), ("讘", "Bet"), ("讙", "Gimel"), ("讚", "Dalet"),
("讛", "He"), ("讜", "Vav"), ("讝", "Zayin"), ("讞", "Het"),
("讟", "Tet"), ("讬", "Yod"), ("讻", "Kaf"), ("诇", "Lamed"),
("诪", "Mem"), ("谞", "Nun"), ("住", "Samekh"),("注", "Ayin"),
("驻", "Pe"), ("爪", "Tsadi"), ("拽", "Qof"), ("专", "Resh"),
("砖", "Shin"), ("转", "Tav")
]
# --- Session State Variables ---
# 1) A shuffled list of all letters for testing
# 2) The index of the current letter
# 3) The user鈥檚 multiple choice guess (A, B, C, or D)
# 4) The list of four choices for the current question
# 5) Whether the user has submitted the answer
# 6) Feedback message to show if correct or wrong
# 7) Track score (optional, if you want to show correct count)
if "random_letters" not in st.session_state:
st.session_state.random_letters = random.sample(HEBREW_LETTERS, len(HEBREW_LETTERS))
if "current_index" not in st.session_state:
st.session_state.current_index = 0
if "choices" not in st.session_state:
st.session_state.choices = []
if "answer_submitted" not in st.session_state:
st.session_state.answer_submitted = False
if "feedback" not in st.session_state:
st.session_state.feedback = ""
if "selected_option" not in st.session_state:
st.session_state.selected_option = None
if "score" not in st.session_state:
st.session_state.score = 0
# --- Helper Functions ---
def generate_choices(correct_name, all_letters):
"""
Returns a list of exactly 4 possible answers (one correct + three random incorrect),
then shuffles them.
"""
incorrect_names = [name for (letter, name) in all_letters if name != correct_name]
# Pick three random wrong answers
three_wrong = random.sample(incorrect_names, 3)
all_options = [correct_name] + three_wrong
random.shuffle(all_options)
return all_options
def load_new_question():
"""
Loads the next question by resetting the answer submission state,
generating new multiple-choice options, and clearing feedback.
"""
st.session_state.answer_submitted = False
st.session_state.feedback = ""
st.session_state.selected_option = None
current_letter, current_name = st.session_state.random_letters[st.session_state.current_index]
st.session_state.choices = generate_choices(current_name, HEBREW_LETTERS)
def check_answer():
"""
Checks if the selected_option is the correct name of the current letter.
Updates feedback and optionally increments score.
"""
current_letter, current_name = st.session_state.random_letters[st.session_state.current_index]
if st.session_state.selected_option == current_name:
st.session_state.feedback = "Correct! You鈥檙e crushing it, unstoppable problem-solver!"
st.session_state.score += 1
else:
st.session_state.feedback = (
f"Wrong! The correct answer is '{current_name}'. "
"Don鈥檛 sweat it鈥攔eal champions learn fast."
)
st.session_state.answer_submitted = True
def next_letter():
"""
Moves to the next letter, or reshuffles if we've reached the end.
Calls load_new_question() to generate new MC options.
"""
if st.session_state.current_index < len(st.session_state.random_letters) - 1:
st.session_state.current_index += 1
else:
st.session_state.current_index = 0
st.session_state.random_letters = random.sample(HEBREW_LETTERS, len(HEBREW_LETTERS))
load_new_question()
# --- Main App Execution ---
st.title("Hebrew Alphabet Tester鈥擭o Nonsense, All Mastery")
# Generate choices once on first run or when next_letter is called
if not st.session_state.choices:
load_new_question()
# Display the current letter
current_letter, current_name = st.session_state.random_letters[st.session_state.current_index]
st.markdown(f"### Letter: {current_letter}")
# Multiple-choice options
if not st.session_state.answer_submitted:
# Radio button for the 4 choices (A, B, C, D)
st.session_state.selected_option = st.radio(
"Select the correct name:",
st.session_state.choices,
index=0
)
if st.button("Check Answer"):
check_answer()
else:
# Show feedback after submission
st.write(st.session_state.feedback)
# Optional: display current score
st.write(f"Score: {st.session_state.score}/{st.session_state.current_index + 1}")
if st.button("Next Letter"):
next_letter()