How to resolve this error in Streamlit app

Question:

I am coding in Python to be run on streamlit, when I use IF argument, it give me error.

import streamlit as st
import pandas as pd

st.write("Welcome to my Sample Suvery App!")

age = st.text_input("What is your age?",
              "Please enter your age here correctly",)
if age > 12:
    name = st.text_input("What is your full name:"
                    "Enter your full name")
    sex = st.selectbox("Male", "Female")
    phone_number = st.number_input("Enter your phone number:")
    father_name = st.text_input("what's your father name?"
                                "Enter father's name")
    mother_name = st.text_input("what's your mother name?"
                                "Enter Mother's name")
    address = st.text_input("where do you live? "
                            "Enter your current address")
while age <= 12:
    st.write("You are not qualify for this suvrey")

    break

st.button("submit", type="primary")

result = st.write(f"{name}")
st.write(f"{result}," "Thanks for your time")

Answers:

The main issue is this line:

age = st.text_input("What is your age?",
              "Please enter your age here correctly",)

This sets age = "Please enter your age here correctly" when the app first renders, which will cause a TypeError because the app will then immediately try to run if age > 12.

I think it would make more sense to set the initial value to an empty string "", and use the argument placeholder="Please enter your age here correctly". I went ahead and made this change to your other st.text_input widgets as well, and added some basic error handling so that the user cannot enter non-numeric values for the age.

I also structured the app so that the other text inputs don’t show up until the age is entered correctly (you can modify this as you like).

import streamlit as st
import pandas as pd

st.write("Welcome to my Sample Survey App!")

age = st.text_input("What is your age?",
            value="",
            placeholder="Please enter your age here correctly",)

## basic error handling
if age != "":
    try: 
        age = float(age)
        if age > 12:
            name = st.text_input("What is your full name:",
                            value="",
                            placeholder="Enter your full name")
            sex = st.selectbox(label="Gender", options=["Male", "Female"])
            phone_number = st.text_input("Enter your phone number:")
            father_name = st.text_input("what's your father name?",
                                        placeholder="Enter father's name")
            mother_name = st.text_input("what's your mother name?",
                                        placeholder="Enter Mother's name")
            address = st.text_input("where do you live? ",
                                    placeholder="Enter your current address")

            if name != "":
                if st.button("submit", type="primary"):
                    st.write(f"{name}, " "Thanks for your time")
        else:
            st.write("You are not qualified for this suvrey")
    except:
        st.error("Please enter a valid age")

enter image description here

Answered By: Derek O
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.