Text_input inside while loop DuplicateWidgetID

I’m trying to create a chatbot with messaging style UI. The chatbot works currently by writing to the console. I’m trying to adapt it to Streamlit. My current code in question is as follows:

    current_line_number = 1
    user_submission = st.text_input('Response', key=str(current_line_number))
    if user_submission = 'exit':
        response = st.write('You said ' + user_submission)
        current_line_number = current_line_number + 1

However, even though I’m changing the keys with each loop, I still get the DuplicateWidgetID error.

Does anyone know what the problem is?

Hi @acarv468. Welcome to the community!

The problem seems to be that you need to move the line number initialization

current_line_number = 1

out of the while loop.

Happy app creating!

Thank you for responding so quickly. That did work solve the DuplicateWidgetID issue. However, now it displays infinite text input boxes. I’ll have to play around with it to try to get it working.

Yes. That’s because the first time your script runs, the text boxes will never have “exit” in them. What are you trying to do?

Also FWIW in Python, that first line would typically read:

while flag:

Happy app creating! :slight_smile:

1 Like

I’m trying to deploy a chatbot. So trying to print a new message each time the user enters text

Can you please help me with this error

Hi @jaiyesh -

Can you please copy your code into the message as plain text? It’s not reasonable to have people type your whole example out in order to help you :slight_smile:


import numpy as np
import streamlit as st

st.title("Guess The Number")
st.markdown("## Welcome to the game of deciding number")
st.sidebar.title("Guess The Number")
st.sidebar.markdown("## Welcome to the game of deciding number")

st.sidebar.write("What is your name mate")
name = st.sidebar.text_input("Enter your name")

if  st.sidebar.checkbox("Show Instructions", False):
    st.write("Nice to meet you", name)
    st.write("Let's play a game, i will guess a number between 1 and 20 and you have guess it in less than 6 guesses")

number = np.random.randint(1,20)

g = 0

while True :
    if g
    if x> number:
        st.write("Wrong, Your guess is greater than the number")
    if x< number:
        st.write("Wrong, Your guess is lesser than the number")
    if x==number :
        st.success("Congratulations, you won")