Add a number to a list under st.radio

I have 3 lists: type_1,type_2, type_3
I want to create a yes/no questionnaire of 10 questions. For each question if the answer is yes I want to add 1 to one of the lists above(let’s say type_1) and at the end I want to show the name of the list which has the highest sum.

I can’t add 1 to let’s say type_1 list after the first question.

What I’ve done is :
answers = [‘yes’, ‘no’]
q_1 = st.radio(‘1.Are you a discipline person?’, answers)

if q_1 == ‘yes’:
type_1.append(1)
else:
type_1.append(0)

I couldn’t add 1 to type_1 list after yes answer

Could you please help me?

​​

Have a look on streamlit’s main concept especially the Data Flow.

From your code, add a key to your radio widget.

Example

import streamlit as st

type_1 = []

ANSWERS = ['yes', 'no']

questions_type_1 = [
    "1. Are you a disciplined person?",
    "2. Do you often follow a daily routine?",
    "3. Are you detail-oriented in your work?"
]

for question in questions_type_1:
    response = st.radio(
        label=question,
        options=ANSWERS,
        key=question,  # here
        horizontal=True
    )
    type_1.append(1 if response == 'yes' else 0)

st.write(type_1)

Output

image

Thank you so much nerdy

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.