0

I want to fix the value of text for the whole code and refresh it when the website is reloaded. The value of text is keep on changing.

import streamlit as st
import random

word_list = ['tuple' , 'global' , 'list' , 'dict' , 'string' , 'elif' , 'else' , 'while' , 'for' , 'import' , 'from' , 'def' , 'try' , 'except' , 'finally']

def random_word():
    
    final_word = random.choice(word_list)
    return final_word

st.set_page_config("Hangman for Python Programmers")

st.markdown("<h1 style='text-align: center; color: red;'>Python Hangman</h1>", unsafe_allow_html=True)


st.write("Enter the blanks with a Python Keyword")

c = st.container()
st.write("Fill letters here")

text = random_word()
  • You use random so the value will change. Tell us more about your problem. – ferdy Mar 21 '22 at 23:41
  • It's a hangman game for python programmers where you need to guess the word which is a python keyword. I have stored the words in word_list and using random module select a random word from the list, the problem is that the random word gets updated when I start entering the letters. Is that possible to keep that one random word fixed for the whole code runtime and not update it every time ? – Navin Chandra Mar 22 '22 at 14:33
  • You can save the text to session_state. Read it at https://docs.streamlit.io/library/api-reference/session-state – ferdy Mar 22 '22 at 18:04
  • Could you please explain how could I store 'text' in session_state, I can't figure it out from the API references. – Navin Chandra Mar 25 '22 at 13:10

0 Answers0