|
import streamlit as st |
|
import importlib |
|
|
|
|
|
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", |
|
} |
|
|
|
|
|
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 |
|
|
|
|
|
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") |
|
|
|
|
|
st.title(title) |
|
st.write(description) |
|
|
|
|
|
st.markdown("<br><br>", unsafe_allow_html=True) |
|
|
|
|
|
question, options, correct_answer, explanation = generate_question() |
|
st.write(f"**Question:** {question}") |
|
answer = st.radio("Choose an answer:", options) |
|
|
|
|
|
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}") |
|
|
|
|
|
col1, col2 = st.columns(2) |
|
if col1.button("Previous Question"): |
|
st.experimental_rerun() |
|
if col2.button("Next Question"): |
|
st.experimental_rerun() |
|
|
|
|
|
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.") |
|
|