File size: 4,157 Bytes
cf41613
31ec766
b51a04b
 
9a8d309
b51a04b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c620f02
b51a04b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b2d386f
b51a04b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b2d386f
b51a04b
 
 
 
 
 
b2d386f
 
 
 
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import streamlit as st
import pandas as pd

def main():
    st.title("Quiz - Bahria College")
    st.write("Create quizzes, collect responses, and view results!")

    # Sidebar for navigation
    menu = ["Create Quiz", "Take Quiz", "View Results"]
    choice = st.sidebar.selectbox("Menu", menu)

    if choice == "Create Quiz":
        create_quiz()
    elif choice == "Take Quiz":
        take_quiz()
    elif choice == "View Results":
        view_results()

def create_quiz():
    st.header("Create a New Quiz")

    # Input for quiz name
    quiz_name = st.text_input("Quiz Name")

    if "quizzes" not in st.session_state:
        st.session_state.quizzes = {}

    if quiz_name:
        if quiz_name not in st.session_state.quizzes:
            st.session_state.quizzes[quiz_name] = []

        st.write(f"Editing quiz: {quiz_name}")

        # Question form
        with st.form(key="question_form"):
            question = st.text_input("Question")
            question_type = st.selectbox("Question Type", ["Multiple Choice", "Short Answer"])

            if question_type == "Multiple Choice":
                options = st.text_area("Options (comma-separated)")
                correct_option = st.text_input("Correct Option")
            else:
                options = None
                correct_option = None

            submitted = st.form_submit_button("Add Question")

            if submitted and question:
                st.session_state.quizzes[quiz_name].append({
                    "question": question,
                    "type": question_type,
                    "options": options.split(",") if options else [],
                    "correct": correct_option,
                })
                st.success("Question added successfully!")

        # Display added questions
        st.subheader("Questions")
        for idx, q in enumerate(st.session_state.quizzes[quiz_name]):
            st.write(f"{idx + 1}. {q['question']}")

    else:
        st.warning("Please enter a quiz name to start.")

def take_quiz():
    st.header("Take a Quiz")

    if "quizzes" not in st.session_state or not st.session_state.quizzes:
        st.warning("No quizzes available. Please create a quiz first.")
        return

    quiz_name = st.selectbox("Select a Quiz", list(st.session_state.quizzes.keys()))
    responses = []

    if quiz_name:
        st.write(f"Quiz: {quiz_name}")
        quiz = st.session_state.quizzes[quiz_name]

        for idx, q in enumerate(quiz):
            st.write(f"{idx + 1}. {q['question']}\n")

            if q['type'] == "Multiple Choice":
                answer = st.radio("", q['options'], key=f"question_{idx}")
            else:
                answer = st.text_input("Your Answer", key=f"question_{idx}")

            responses.append(answer)

        if st.button("Submit Quiz"):
            if "results" not in st.session_state:
                st.session_state.results = {}

            st.session_state.results[quiz_name] = responses
            st.success("Quiz submitted successfully!")

def view_results():
    st.header("View Results")

    if "results" not in st.session_state or not st.session_state.results:
        st.warning("No results available.")
        return

    quiz_name = st.selectbox("Select a Quiz", list(st.session_state.results.keys()))

    if quiz_name:
        st.write(f"Results for: {quiz_name}")
        results = st.session_state.results[quiz_name]

        quiz = st.session_state.quizzes[quiz_name]
        correct_count = 0

        for idx, (response, question) in enumerate(zip(results, quiz)):
            st.write(f"Q{idx + 1}: {question['question']}")
            st.write(f"Your Answer: {response}")

            if question['type'] == "Multiple Choice" and response == question['correct']:
                correct_count += 1
            elif question['type'] == "Short Answer" and response and question['correct'] and response.lower() == question['correct'].lower():
                correct_count += 1

        st.write(f"Score: {correct_count}/{len(quiz)}")

if __name__ == "__main__":
    main()

# requirements.txt
# streamlit==1.25.0
# pandas==1.5.3