learnhebrew / app.py
admin08077's picture
Update app.py
524befa verified
import streamlit as st
import random
# We’re 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’ll 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’s 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’re crushing it, unstoppable problem-solver!"
st.session_state.score += 1
else:
st.session_state.feedback = (
f"Wrong! The correct answer is '{current_name}'. "
"Don’t sweat it—real 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—No 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()