Spaces:
Runtime error
Runtime error
| import streamlit as st | |
| import random | |
| def app(): | |
| st.title("Fitness Knowledge Quiz") | |
| # Initialize session state for quiz | |
| if 'quiz_started' not in st.session_state: | |
| st.session_state.quiz_started = False | |
| if 'current_question' not in st.session_state: | |
| st.session_state.current_question = 0 | |
| if 'score' not in st.session_state: | |
| st.session_state.score = 0 | |
| if 'quiz_completed' not in st.session_state: | |
| st.session_state.quiz_completed = False | |
| if 'questions' not in st.session_state: | |
| # Sample quiz questions | |
| st.session_state.questions = [ | |
| { | |
| "question": "What is the recommended amount of water to drink daily for an average adult?", | |
| "options": ["2-3 cups", "4-5 cups", "8-10 cups", "15-16 cups"], | |
| "correct": 2, # 8-10 cups | |
| "explanation": "The U.S. National Academies of Sciences, Engineering, and Medicine determined that adequate daily fluid intake is about 15.5 cups (3.7 liters) for men and about 11.5 cups (2.7 liters) for women. This includes fluids from water, beverages and food, with about 8-10 cups coming specifically from drinks." | |
| }, | |
| { | |
| "question": "Which of these exercises is primarily for cardiovascular fitness?", | |
| "options": ["Bench press", "Running", "Bicep curls", "Planks"], | |
| "correct": 1, # Running | |
| "explanation": "Running is primarily a cardiovascular exercise that elevates your heart rate and improves cardiorespiratory endurance. The other options are mainly strength or stability exercises." | |
| }, | |
| { | |
| "question": "What is the primary nutrient for building muscle?", | |
| "options": ["Carbohydrates", "Fat", "Protein", "Vitamin C"], | |
| "correct": 2, # Protein | |
| "explanation": "Protein provides the building blocks (amino acids) necessary for muscle repair and growth. While carbohydrates fuel workouts and fats support hormone production, protein is directly involved in the muscle-building process." | |
| }, | |
| { | |
| "question": "Which exercise helps strengthen your core?", | |
| "options": ["Squats", "Bicep curls", "Planks", "Jumping jacks"], | |
| "correct": 2, # Planks | |
| "explanation": "Planks primarily target the core muscles, including the transverse abdominis, rectus abdominis, obliques, and lower back muscles. They're excellent for building core stability and strength." | |
| }, | |
| { | |
| "question": "What is HIIT?", | |
| "options": ["Heavy Intensity Interval Training", "High Impact Interval Training", "High Intensity Interval Training", "Hybrid Intensity Interval Training"], | |
| "correct": 2, # High Intensity Interval Training | |
| "explanation": "HIIT stands for High Intensity Interval Training. It involves short bursts of intense exercise alternated with low-intensity recovery periods, which is an efficient way to improve cardiovascular fitness and burn calories." | |
| } | |
| ] | |
| # Display quiz introduction if not started | |
| if not st.session_state.quiz_started and not st.session_state.quiz_completed: | |
| st.markdown(""" | |
| ### Test your fitness knowledge! | |
| This quiz contains 5 questions about fitness, nutrition, and health. | |
| * Each correct answer is worth 1 point | |
| * Try to answer without searching online | |
| * You'll get explanations for each answer | |
| """) | |
| if st.button("Start Quiz"): | |
| st.session_state.quiz_started = True | |
| st.session_state.current_question = 0 | |
| st.session_state.score = 0 | |
| st.session_state.quiz_completed = False | |
| st.rerun() | |
| # Display current question | |
| elif st.session_state.quiz_started and not st.session_state.quiz_completed: | |
| current_q = st.session_state.questions[st.session_state.current_question] | |
| st.subheader(f"Question {st.session_state.current_question + 1} of {len(st.session_state.questions)}") | |
| st.write(current_q["question"]) | |
| # User selection | |
| answer = st.radio("Select your answer:", current_q["options"], key=f"q{st.session_state.current_question}") | |
| selected_index = current_q["options"].index(answer) | |
| col1, col2 = st.columns(2) | |
| with col1: | |
| if st.button("Submit Answer"): | |
| if selected_index == current_q["correct"]: | |
| st.success("Correct! 🎉") | |
| st.session_state.score += 1 | |
| else: | |
| st.error("Incorrect. The correct answer is: " + current_q["options"][current_q["correct"]]) | |
| st.info(current_q["explanation"]) | |
| # Move to next question or end quiz | |
| if st.session_state.current_question < len(st.session_state.questions) - 1: | |
| st.session_state.current_question += 1 | |
| st.button("Next Question", on_click=st.rerun) | |
| else: | |
| st.session_state.quiz_completed = True | |
| st.session_state.quiz_started = False | |
| st.button("See Results", on_click=st.rerun) | |
| with col2: | |
| if st.button("Skip Question"): | |
| if st.session_state.current_question < len(st.session_state.questions) - 1: | |
| st.session_state.current_question += 1 | |
| else: | |
| st.session_state.quiz_completed = True | |
| st.session_state.quiz_started = False | |
| st.rerun() | |
| # Display quiz results | |
| elif st.session_state.quiz_completed: | |
| st.subheader("Quiz Results") | |
| # Calculate percentage | |
| percentage = (st.session_state.score / len(st.session_state.questions)) * 100 | |
| st.write(f"You scored: {st.session_state.score} out of {len(st.session_state.questions)} ({percentage:.1f}%)") | |
| # Show performance message based on score | |
| if percentage >= 80: | |
| st.success("Great job! You have excellent fitness knowledge! 💪") | |
| elif percentage >= 60: | |
| st.info("Good work! You have a solid understanding of fitness concepts.") | |
| else: | |
| st.warning("There's room for improvement. Keep learning about fitness!") | |
| # Option to retry quiz | |
| if st.button("Take Quiz Again"): | |
| st.session_state.quiz_started = False | |
| st.session_state.current_question = 0 | |
| st.session_state.score = 0 | |
| st.session_state.quiz_completed = False | |
| # Shuffle questions for variety | |
| random.shuffle(st.session_state.questions) | |
| st.rerun() | |