Spaces:
Sleeping
Sleeping
import streamlit as st | |
import re | |
def parse_mcq_questions(mcq_list): | |
# Split the string into individual questions | |
questions = re.split(r'\d+\.\s+', mcq_list)[1:] # Skip the empty first element | |
parsed_questions = [] | |
for q in questions: | |
# Split into question and options | |
parts = q.strip().split(' - ') | |
question = parts[0].strip() | |
options = { | |
opt[0]: opt[2:].strip() | |
for opt in parts[1:] | |
} | |
parsed_questions.append({ | |
'question': question, | |
'options': options | |
}) | |
return parsed_questions | |
def main(): | |
st.title("Multiple Choice Quiz") | |
# Initialize session state variables if they don't exist | |
if 'current_question' not in st.session_state: | |
st.session_state.current_question = 0 | |
if 'answers' not in st.session_state: | |
st.session_state.answers = [] | |
if 'questions' not in st.session_state: | |
# Your MCQ string goes here | |
mcq_list = """1. Which data analysis technique is primarily used to uncover underlying patterns and trends in data? | |
- A) Data Cleaning | |
- B) Exploratory Data Analysis | |
- C) Database Management | |
- D) System Testing | |
2. What does "Data Cleaning" refer to in the context of data analysis? | |
- A) Removing irrelevant data | |
- B) Transforming data types | |
- C) Handling missing values and inconsistencies | |
- D) All of the above | |
3. What is the purpose of foreign key constraints in database management? | |
- A) To ensure data integrity and relationships between tables | |
- B) To speed up query execution | |
- C) To encrypt sensitive data | |
- D) To define primary keys | |
4. Which of the following is a key aspect of "Data Science"? | |
- A) Building machine learning models | |
- B) Extracting insights from data | |
- C) Visualizing data | |
- D) All of the above | |
5. What is a primary goal of "relevant analytics experience"? | |
- A) Gathering data from various sources | |
- B) Deriving actionable insights from data | |
- C) Building complex machine learning models | |
- D) Managing large databases | |
6. "Data Analytics" typically involves which of the following? | |
- A) Statistical analysis | |
- B) Data visualization | |
- C) Data interpretation | |
- D) All of the above | |
7. What are "large data sets" often referred to as? | |
- A) Big Data | |
- B) Small Data | |
- C) Medium Data | |
- D) Tiny Data | |
8. "Data Sources" can include which of the following? | |
- A) Databases | |
- B) APIs | |
- C) CSV files | |
- D) All of the above | |
9. What is the purpose of "machine learning models"? | |
- A) To predict outcomes based on data | |
- B) To store data efficiently | |
- C) To visualize data relationships | |
- D) To clean and preprocess data | |
10. What is a key aspect of "machine learning systems deployment"? | |
- A) Integrating models into real-world applications | |
- B) Training machine learning models | |
- C) Collecting and labeling data | |
- D) Evaluating model performance | |
""" | |
st.session_state.questions = parse_mcq_questions(mcq_list) | |
# Display current question number and total questions | |
st.write(f"Question {st.session_state.current_question + 1} of {len(st.session_state.questions)}") | |
# Display current question | |
current_q = st.session_state.questions[st.session_state.current_question] | |
st.write(current_q['question']) | |
# Create radio buttons for options with the corrected format_func | |
answer = st.radio( | |
"Select your answer:", | |
options=['A', 'B', 'C', 'D'], # List of option keys | |
format_func=lambda x: f"{x}) {current_q['options'].get(x, ' ')}", | |
key=f"question_{st.session_state.current_question}" # Unique key per question | |
) | |
# Navigation buttons in columns | |
col1, col2 = st.columns(2) | |
with col1: | |
if st.session_state.current_question > 0: | |
if st.button("Previous"): | |
st.session_state.current_question -= 1 | |
if st.session_state.answers: # Only remove answer if there are answers | |
st.session_state.answers = st.session_state.answers[:-1] | |
st.rerun() | |
with col2: | |
if st.session_state.current_question < len(st.session_state.questions) - 1: | |
if st.button("Next"): | |
st.session_state.answers.append(f"{st.session_state.current_question + 1}-{answer}") | |
st.session_state.current_question += 1 | |
st.rerun() | |
elif st.button("Submit"): | |
st.session_state.answers.append(f"{st.session_state.current_question + 1}-{answer}") | |
st.write("Quiz completed! Your answers:") | |
st.write(st.session_state.answers) | |
# Add a restart button | |
if st.button("Restart Quiz"): | |
st.session_state.current_question = 0 | |
st.session_state.answers = [] | |
st.rerun() | |
if __name__ == "__main__": | |
main() |