A Multiple Choice Quiz

Hey everyone - please forgive me as I have not been coding for very long. I have been trying to code a multiple choice quiz app based on my construction education blog I have been creating. I have been trying to learn the session_state feature to try and ensure that the quiz displays one multiple choice st.radio question at a time, moves onto the next question once the submit button is clicked and then tally’s up and displays the score at the end. I would be super grateful for any support as I cannot quite get it to work!

Please assume the structure of quiz_data to be a list of dictionaries containing question, choices and answer as keys…

import streamlit as st
from quiz_data import quiz_data

# Function to display the current question and choices
def show_question():
    question = quiz_data[st.session_state.current_question] 
    st.write(question['question']) # Selects the question within the dictionary being accessed by previous line within the quiz_data list.
    selected_choice = st.radio("Select your answer:", question['choices']) 
    submit_button = st.button("Submit") 
    if submit_button:
        st.session_state.show_question = False

# Function to check the selected answer and provide feedback
def check_answer(selected_choice):
    question = quiz_data[st.session_state.current_question]
    if selected_choice == question["answer"]:
        st.session_state.score += 1

# function to move onto the next question
def next_question():
    current_question = st.session_state.current_question + 1
    if current_question < len(quiz_data): 
        st.session_state.current_question = current_question 
        st.session_state.show_question = True 
        st.success("Quiz Complete! Your Score: {}/{}".format(st.session_state.score, len(quiz_data)))

# Main function - initialising the 3 session state variables.
def main():
    if 'current_question' not in st.session_state:
        st.session_state.current_question = 0 
    if 'score' not in st.session_state:
        st.session_state.score = 0
    if 'show_question' not in st.session_state:
        st.session_state.show_question = True

    st.title("Piling Quiz")

    if st.session_state.show_question:

if __name__ == "__main__":

Hello @Joe.Posnett, and welcome to our community! :hugs:

It looks like your approach is on the right track. However, there might be a few additional elements you could incorporate into your session state logic to improve it.

Happy to assist further but could you please share the quiz data with me?


Hi Charly_Wargnier,

Thank you so much for reaching out so quickly!

See below what I have in the quiz_data.py page:

import streamlit as st

quiz_data = [
“question”: “1. What is the purpose of pile probing?”,
“choices”: [
“a) To save money”,
“b) To check for potential voids in proposed pile location”,
“c) To check the conditions of the surrounding soil”,
“d) To ensure they have been installed correctly”
“answer”: “b) To check for potential voids in proposed pile location”
“question”: “2. How does a pile integrity test work?”,
“choices”: [
“a) Through electromagnetic waves”,
“b) By breaking a small piece of the concrete poured”,
“c) Through engineering calculations”,
“d) Sending stress waves through the pile using a hand-held device or sensor”
“answer”: “d) Sending stress waves through the pile using a hand-held device or sensor”

Thanks @Joe.Posnett

Thinking about it, @sebastiandres didn’t you have a component for this?


Yes! streamlit-book to the rescue!
You can install directly from pypi
The documentation is here: Interactive Activities — streamlit_book documentation
And a demo is here: https://stbook-multipaging.streamlit.app/book and here https://stbook-methods.streamlit.app/

Hope it helps!

Brilliant, thanks @sebastiandres! :raised_hands:

Hey guys that is amazing and definitely makes life a lot easier!

I played around with this for a while but I still have the issue of wanting the quiz to show one question at a time with the next one loaded each time the “submit” button is pressed. I also want to display the tally of a score on the last page (as per my attempt above).

What would the best way to do this be?

Thank you for the help and patience!

After a solid 2 hours this morning I managed to solve it exactly how I wanted it! Whilst it was my own code in the end it was very much inspired by the Streamlit-book links you sent over so thank you both!



Glad you managed to create what you were after, @Joe.Posnett!

And Kudos to @sebastiandres for creating such a wonderful component! :raised_hands: