File size: 2,882 Bytes
7ba3b4e
d924141
 
32f2e18
d924141
32f2e18
 
 
 
 
 
 
 
2a56f14
 
32f2e18
 
 
 
 
 
 
 
2a56f14
32f2e18
 
d924141
 
 
 
 
 
32f2e18
 
 
 
 
 
d924141
32f2e18
 
 
 
 
d924141
 
32f2e18
 
d924141
32f2e18
d924141
32f2e18
d924141
 
 
32f2e18
 
 
 
 
 
 
 
 
 
 
 
 
 
d924141
 
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
import streamlit as st
import importlib

# List of available modules with shorter names and icons
module_names = {
    "Bases": "presentation_bases",
    "Validity": "valid_invalid_numbers",
    "Conversion": "conversion_bases",
    "Grouping": "grouping_techniques",
    "Addition": "addition_bases",
    "2's Complement": "twos_complement",
    "Negative Numbers": "negative_binary",
    "Subtraction": "subtraction_bases",
}

# Initialize session state for tracking answers and progress
if 'correct_count' not in st.session_state:
    st.session_state.correct_count = 0
if 'question_count' not in st.session_state:
    st.session_state.question_count = 0
if 'current_module' not in st.session_state:
    st.session_state.current_module = None

# Streamlit interface
st.sidebar.title("Quiz Modules")
module_name = st.sidebar.radio("Choose a module:", list(module_names.keys()), index=0)

if module_name:
    module_file = module_names[module_name]
    try:
        module = importlib.import_module(f'modules.{module_file}')
        generate_question = module.generate_question
        title = getattr(module, 'title', f"{module_name} Module")
        description = getattr(module, 'description', "Description is not available")
        
        # Show module title and description
        st.title(title)
        st.write(description)
        
        # Add spacing to ensure question visibility on Hugging Face
        st.markdown("<br><br>", unsafe_allow_html=True)

        # Generate and display the question
        question, options, correct_answer, explanation = generate_question()
        st.write(f"**Question:** {question}")
        answer = st.radio("Choose an answer:", options)

        # Track user's response
        if st.button("Submit"):
            st.session_state.question_count += 1
            if answer == correct_answer:
                st.session_state.correct_count += 1
                st.success("Correct!", icon="βœ…")
            else:
                st.error("Incorrect.", icon="❌")
            st.write(f"**Explanation:** {explanation}")

        # Option to go to the next or previous question
        col1, col2 = st.columns(2)
        if col1.button("Previous Question"):
            st.experimental_rerun()  # To simulate going back (if history is managed)
        if col2.button("Next Question"):
            st.experimental_rerun()  # To simulate moving forward

        # Show percentage of correct answers
        if st.session_state.question_count > 0:
            correct_percentage = (st.session_state.correct_count / st.session_state.question_count) * 100
            st.write(f"**Correct Answers:** {st.session_state.correct_count}/{st.session_state.question_count} ({correct_percentage:.2f}%)")
        
    except ModuleNotFoundError:
        st.error(f"The module '{module_name}' was not found. Please select another module.")