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:
check_answer(selected_choice)
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.write("Correct!")
st.balloons()
st.session_state.score += 1
else:
st.write("Incorrect!")
next_question()
# 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
else:
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:
show_question()
else:
next_question()
if __name__ == "__main__":
main()
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?
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”
}
]
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).
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!
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.