If elif statement

Hi I received an error message about ‘<=’ not supported between instances of ‘DeltaGenerator’ and ‘float’. Below is my code. There is only float in BMI comparing INDEXp. There is no string. Not sure why it doesnt work. Please assist


BMI = round(52/1.54**2,1)
INDEXp = [18.5,23.0,27.5]
INDEXd = [“under_weight”,“normal_weight”,“over_weight”,“obesity”]
INDEXa = [“not so good”,“good”,“bad”]

if BMI<=INDEXp[0]:
	LEVEL=[INDEXd[0],INDEXa[0]]
elif BMI>INDEXp[0] and BMI<=INDEXp[1]:
	LEVEL=[INDEXd[1],INDEXa[1]]
else:
	st.text("Error")

@AliceKoh Can you please print the type of BMI and INDEXp

print(f"Type of BMI: {type(BMI)}")
print(f"Type of INDEXp: {type(INDEXp)}")

Hi Attached shown that there is no string.

@AliceKoh Thank you for printing them but can you print them inside the streamlit script. I want to see the type of variables before it gives an error.

Maybe something part of the code is changing the type of these variables.

HI gaganmanku96,
Oh I am using sublime text and jupyter. May I know what is the name of the software that I can type streamlit script ? I will download and try

Hi gaganmanku96,
Are you referring to this where I got it from the error message (pink)?

File "C:\Users\alice\anaconda3\lib\site-packages\streamlit\scriptrunner\script_runner.py", line 557, in _run_script
    exec(code, module.__dict__)File "display.py", line 108, in <module>
    main()File "display.py", line 64, in main
    if BMI<=INDEXp[0]:

Yes, exactly.

HI I tried to use another method but also encounter error. I really hope someone can help with this as if elif else statement with value is a very common yet powerful statement. I feel that I am handicap without this function. Please assist. Thanks.

File "C:\Users\alice\anaconda3\lib\site-packages\streamlit\scriptrunner\script_runner.py", line 557, in _run_script
    exec(code, module.__dict__)File "display.py", line 143, in <module>
    main()File "display.py", line 140, in main
    rpt_bmi(BMI,fname)File "display.py", line 46, in rpt_bmi
    cal_bmi(BMI)File "display.py", line 17, in cal_bmi
    if BMI <Index_BMI[0] or BMI ==Index_BMI[0]:

This is my modified code

Index_BMI = [18.5,23,27.5]
Index_descrip = [“under weight”,“normal weight”,“over weight”,“obesity”]

if BMI <Index_BMI[0] or BMI ==Index_BMI[0]:
	Level = [Index_descrip[0],Index_result[0]]

elif BMI >Index_BMI[0]: 
	if BMI <Index_BMI[1] or BMI ==Index_BMI[1]:
		Level = [Index_descrip[1],Index_result[1]]

	elif BMI >Index_BMI[1]: 
		if BMI <Index_BMI[2] or BMI ==Index_BMI[2]:
			Improve = Best[2]
			Level = [Index_descrip[2],Index_result[2]]

else:
	Level = [Index_descrip[3],Index_result[3]]

Hi @AliceKoh - welcome to Streamlit!

I’ve read through this thread and it’s difficult to see what your problem is exactly. On the one hand you’re talking about Jupyter and on the other hand saying you wish to run that app in Streamlit. The two platforms are quite different and apps for each one are built differently. Also the code snippets you have shown are incomplete / incorrect. Anyhow, I figured you’re building a BMI calculator and since I’m trying to lose weight :slight_smile: I thought I’d experiment with your code to make it work in Streamlit.

I found a BMI calculator on the web and replicated it in Streamlit. You can see my version of the app here.

I’ve put the code on GitHub with full instructions so you can start playing with Streamlit.

HTH,
Arvindra

2 Likes

Hi Arvindra,
Thank you for helping. Your BMI calculator looks nice. May I know how did you define with outside_display_area: ? I try to run the code but it says need to define.

Please see the code on GitHub. There are a couple of lines of code not displayed with with echo(), including the streamlit import. I renamed outside_display_area to outside_expander_area, which is more accurate.

import streamlit as st

outside_expander_area = st.container()

with st.expander('Show the code', expanded=True):
    with st.echo():
        ...
        ... The rest of the code here ...
        ...

thanks for your assistant. I manage to use the if-elif-else statement now as per your help. Your meter is cool, maybe someday I am going to make one but not now as might be a bit technical for me. So I complete mine in a cheating way
ha ha ha


1 Like

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