If after st.radio seems not to evaluate to True

I am using Community cloud, to make a little game: Select the chemical element from a list, using the radio button, if the answer is correct, add a point, if not substract a point.
The code: Sign in to GitHub · GitHub
The app:
https://appyt-ckozyvp24abfwhpveqcpp5.streamlit.app/

Hello,
You are not ‘waiting’ for the answer of the st.radio.
Currently it’s if “None”==respuesta => False, so it’s always the -1.
And when you trigger the st.radio, it rerun the code.
(With this modification, i think you should do a st.rerun after the loop to have the next question :))

import streamlit as st
import pandas as pd
import random

url = 'https://docs.google.com/spreadsheets/d/e/2PACX-1vTyeAUixFkE9fiDDCx_Zifmngrjf1_9jjr1Tb7n1twPWiw0tfqd0atb1juO9ncpD5wDrjbBgcHqmfOy/pub?gid=435584327&single=true&output=csv'
df = pd.read_csv(url)

#lista=[]

juego = st.button('🔄 Juego nuevo')


if "puntos" not in st.session_state:
    st.session_state.puntos = 0

if "juego_state" not in st.session_state:
    st.session_state.juego_state = False

if juego or st.session_state.juego_state:
    
    st.session_state.juego_state = True
    
    num = random.randint(0, len(df))
    elemento = df.iloc[num]['Elemento']
    
    symbol = df.iloc[num]['Symbol']
    letra = df.iloc[num]['Elemento'][0]
    pistas = df['Elemento'].loc[df['Elemento'].str.startswith(letra)]
    
    lista = pistas.values.tolist()
    
    st.write("¿Cuál es el nombre del elemento químico con el símbolo ",symbol, "?")
    respuesta = st.radio("Selecciona el elemento",lista,index=None)
    st.write(respuesta)
    st.write(lista)
    st.write(elemento)

    if respuesta : #If it's NONE do not change the value.
        if respuesta ==  elemento:
           st.write("¡Excelente!")
           st.session_state.puntos += 1
           st.write("Puntos",st.session_state.puntos)
        else:
            st.write("Respuesta incorrecta")
            st.session_state.puntos -= 1
            st.write("Puntos",st.session_state.puntos)
            st.write(respuesta)
        st.rerun() # Maybe to add to select the next one 
    
    
1 Like

Thank you Faltawer, for your kind reply and code, but still something is missing since even if I select the right answer, the if doesn’t evaluate to True :thinking:

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