amkj84 commited on
Commit
cf41613
Β·
verified Β·
1 Parent(s): 8d46a8d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +149 -0
app.py ADDED
@@ -0,0 +1,149 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import json
3
+ import csv
4
+ import os
5
+ from streamlit_lottie import st_lottie
6
+
7
+ # Load Lottie animation
8
+ def load_lottie_url(url):
9
+ try:
10
+ import requests
11
+ response = requests.get(url)
12
+ if response.status_code != 200:
13
+ return None
14
+ return response.json()
15
+ except ImportError:
16
+ st.error("Please install requests module.")
17
+ return None
18
+
19
+ # Load animations
20
+ animation_quiz = load_lottie_url("https://assets2.lottiefiles.com/packages/lf20_tfb3estd.json")
21
+ animation_success = load_lottie_url("https://assets2.lottiefiles.com/packages/lf20_gs9xr3qn.json")
22
+ animation_welcome = load_lottie_url("https://assets10.lottiefiles.com/packages/lf20_u4yrau.json")
23
+
24
+ # CSV file for saving results
25
+ RESULTS_FILE = "quiz_results.csv"
26
+
27
+ # Function to save results
28
+ def save_results(name, score, total_questions):
29
+ if not os.path.exists(RESULTS_FILE):
30
+ with open(RESULTS_FILE, mode="w", newline="") as file:
31
+ writer = csv.writer(file)
32
+ writer.writerow(["Name", "Score", "Total Questions"])
33
+
34
+ with open(RESULTS_FILE, mode="a", newline="") as file:
35
+ writer = csv.writer(file)
36
+ writer.writerow([name, score, total_questions])
37
+
38
+ # Page Configuration
39
+ st.set_page_config(
40
+ page_title="Respiratory System Quiz",
41
+ layout="centered"
42
+ )
43
+
44
+ # Title and Introduction
45
+ st.title("🌬️ Respiratory System Quiz for Class 6 🌟")
46
+ st_lottie(animation_welcome, height=200, key="welcome")
47
+ st.write("""
48
+ This interactive quiz will test your knowledge of the respiratory system.
49
+ Let's dive in and learn while having fun!
50
+ """)
51
+
52
+ # User Name
53
+ name = st.text_input("Enter your name:", key="username")
54
+ if not name:
55
+ st.warning("Please enter your name to start the quiz.")
56
+ st.stop()
57
+
58
+ # Quiz Questions
59
+ questions = [
60
+ {
61
+ "question": "1. What is the main function of the respiratory system?",
62
+ "options": [
63
+ "To circulate blood",
64
+ "To digest food",
65
+ "To exchange gases (oxygen and carbon dioxide)",
66
+ "To break down nutrients"
67
+ ],
68
+ "answer": "To exchange gases (oxygen and carbon dioxide)"
69
+ },
70
+ {
71
+ "question": "2. Which organ is responsible for taking in oxygen?",
72
+ "options": ["Heart", "Lungs", "Stomach", "Liver"],
73
+ "answer": "Lungs"
74
+ },
75
+ {
76
+ "question": "3. What is the name of the tiny air sacs in the lungs where gas exchange occurs?",
77
+ "options": ["Alveoli", "Bronchi", "Capillaries", "Trachea"],
78
+ "answer": "Alveoli"
79
+ },
80
+ {
81
+ "question": "4. Which muscle helps the lungs during breathing?",
82
+ "options": ["Diaphragm", "Biceps", "Trapezius", "Quadriceps"],
83
+ "answer": "Diaphragm"
84
+ },
85
+ {
86
+ "question": "5. What happens when we exhale?",
87
+ "options": [
88
+ "We take in oxygen",
89
+ "We release oxygen",
90
+ "We release carbon dioxide",
91
+ "We circulate blood"
92
+ ],
93
+ "answer": "We release carbon dioxide"
94
+ },
95
+ {
96
+ "question": "6. What is the function of the trachea?",
97
+ "options": [
98
+ "To pump blood",
99
+ "To carry food",
100
+ "To transport air to the lungs",
101
+ "To digest food"
102
+ ],
103
+ "answer": "To transport air to the lungs"
104
+ },
105
+ {
106
+ "question": "7. What protects the lungs from injury?",
107
+ "options": ["The heart", "The ribs", "The diaphragm", "The spine"],
108
+ "answer": "The ribs"
109
+ },
110
+ ]
111
+
112
+ # Quiz Logic
113
+ st.header("πŸ“ Start the Quiz!")
114
+ st_lottie(animation_quiz, height=150, key="quiz")
115
+ score = 0
116
+ user_answers = []
117
+
118
+ for index, q in enumerate(questions):
119
+ st.subheader(q["question"])
120
+ user_answer = st.radio(f"Choose the correct answer for Question {index + 1}:", q["options"], key=f"q{index}")
121
+ user_answers.append((q["question"], user_answer))
122
+ if user_answer == q["answer"]:
123
+ st.success("βœ… Correct!")
124
+ score += 1
125
+ else:
126
+ st.error(f"❌ Incorrect! The correct answer is: {q['answer']}")
127
+
128
+ # Display Final Score
129
+ if st.button("Show Final Score"):
130
+ st.write(f"**Your final score is: {score}/{len(questions)}** πŸŽ‰")
131
+ if score == len(questions):
132
+ st.balloons()
133
+ st_lottie(animation_success, height=200, key="success")
134
+ st.success("Excellent! You're a respiratory system expert!")
135
+ elif score >= len(questions) // 2:
136
+ st.info("Good job! You have a solid understanding.")
137
+ else:
138
+ st.warning("Keep learning! You can do better next time.")
139
+
140
+ # Save results
141
+ save_results(name, score, len(questions))
142
+ st.write("βœ… Your results have been saved.")
143
+
144
+ # Footer
145
+ st.markdown("""
146
+ ---
147
+ ### About the App
148
+ This app is designed to make learning about the respiratory system fun and engaging for Class 6 students.
149
+ """)