Spaces:
Sleeping
Sleeping
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() |