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