Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import random
|
| 3 |
+
|
| 4 |
+
# Function to generate quiz questions
|
| 5 |
+
def generate_questions():
|
| 6 |
+
questions = {
|
| 7 |
+
"What is the capital of France?": "Paris",
|
| 8 |
+
"What is 2 + 2?": "4",
|
| 9 |
+
"What is the color of the sky?": "Blue",
|
| 10 |
+
"What is the largest mammal?": "Blue Whale",
|
| 11 |
+
"What is the boiling point of water?": "100°C"
|
| 12 |
+
}
|
| 13 |
+
return random.sample(list(questions.items()), 3) # Select 3 random questions
|
| 14 |
+
|
| 15 |
+
# Main function to run the Streamlit app
|
| 16 |
+
def main():
|
| 17 |
+
st.title("Memory Quiz Game")
|
| 18 |
+
|
| 19 |
+
if 'score' not in st.session_state:
|
| 20 |
+
st.session_state.score = 0
|
| 21 |
+
st.session_state.questions = generate_questions()
|
| 22 |
+
st.session_state.current_question = 0
|
| 23 |
+
|
| 24 |
+
if st.session_state.current_question < len(st.session_state.questions):
|
| 25 |
+
question, answer = st.session_state.questions[st.session_state.current_question]
|
| 26 |
+
user_answer = st.text_input(question, "")
|
| 27 |
+
|
| 28 |
+
if st.button("Submit"):
|
| 29 |
+
if user_answer.strip().lower() == answer.lower():
|
| 30 |
+
st.session_state.score += 1
|
| 31 |
+
st.success("Correct!")
|
| 32 |
+
else:
|
| 33 |
+
st.error(f"Wrong! The correct answer was: {answer}")
|
| 34 |
+
|
| 35 |
+
st.session_state.current_question += 1
|
| 36 |
+
else:
|
| 37 |
+
st.write(f"Quiz complete! Your score: {st.session_state.score}/{len(st.session_state.questions)}")
|
| 38 |
+
if st.button("Restart"):
|
| 39 |
+
st.session_state.score = 0
|
| 40 |
+
st.session_state.current_question = 0
|
| 41 |
+
st.session_state.questions = generate_questions()
|
| 42 |
+
|
| 43 |
+
if __name__ == "__main__":
|
| 44 |
+
main()
|