Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
|
3 |
+
# Title of the app
|
4 |
+
st.title("Quiz Maker App")
|
5 |
+
|
6 |
+
# Function to create a quiz
|
7 |
+
def create_quiz():
|
8 |
+
st.header("Create a Quiz")
|
9 |
+
quiz_name = st.text_input("Enter Quiz Name")
|
10 |
+
num_questions = st.number_input("Number of Questions", min_value=1, max_value=20, value=1)
|
11 |
+
|
12 |
+
questions = []
|
13 |
+
for i in range(num_questions):
|
14 |
+
st.subheader(f"Question {i+1}")
|
15 |
+
question = st.text_input(f"Enter Question {i+1}")
|
16 |
+
options = []
|
17 |
+
for j in range(4):
|
18 |
+
option = st.text_input(f"Option {j+1} for Question {i+1}")
|
19 |
+
options.append(option)
|
20 |
+
correct_answer = st.selectbox(f"Correct Answer for Question {i+1}", options)
|
21 |
+
questions.append({
|
22 |
+
"question": question,
|
23 |
+
"options": options,
|
24 |
+
"correct_answer": correct_answer
|
25 |
+
})
|
26 |
+
|
27 |
+
if st.button("Save Quiz"):
|
28 |
+
st.session_state.quiz = {
|
29 |
+
"quiz_name": quiz_name,
|
30 |
+
"questions": questions
|
31 |
+
}
|
32 |
+
st.success("Quiz Saved Successfully!")
|
33 |
+
|
34 |
+
# Function to take a quiz
|
35 |
+
def take_quiz():
|
36 |
+
st.header("Take a Quiz")
|
37 |
+
if "quiz" not in st.session_state:
|
38 |
+
st.warning("No quiz available. Please create a quiz first.")
|
39 |
+
return
|
40 |
+
|
41 |
+
quiz = st.session_state.quiz
|
42 |
+
st.subheader(f"Quiz: {quiz['quiz_name']}")
|
43 |
+
score = 0
|
44 |
+
for i, q in enumerate(quiz["questions"]):
|
45 |
+
st.write(f"Q{i+1}: {q['question']}")
|
46 |
+
user_answer = st.radio(f"Select an answer for Q{i+1}", q["options"], key=f"q{i}")
|
47 |
+
if user_answer == q["correct_answer"]:
|
48 |
+
score += 1
|
49 |
+
|
50 |
+
if st.button("Submit Quiz"):
|
51 |
+
st.success(f"Your score: {score}/{len(quiz['questions'])}")
|
52 |
+
|
53 |
+
# Main app logic
|
54 |
+
def main():
|
55 |
+
st.sidebar.title("Navigation")
|
56 |
+
app_mode = st.sidebar.radio("Choose an option", ["Create Quiz", "Take Quiz"])
|
57 |
+
|
58 |
+
if app_mode == "Create Quiz":
|
59 |
+
create_quiz()
|
60 |
+
elif app_mode == "Take Quiz":
|
61 |
+
take_quiz()
|
62 |
+
|
63 |
+
if __name__ == "__main__":
|
64 |
+
main()
|