+ 1

Can you guys pls help, I put weight as 130 and height as 1.7 and the output is meant to be obesity but it displays normal

weight=int(input()) height=float(input()) cal= (weight /height**2) if cal <18.5: print("Underweight") else: if cal >= 18.5: print("Normal") else: if cal >=25 <30: print("Overweight") else: if cal >30: print("Obesity")

25th Apr 2022, 9:20 AM
Zanele
Zanele - avatar
10 Antworten
+ 4
cal >=25<30 is wrong. maybe you mean 25<=cal<30 and yes, use elif instead of nested if else.
25th Apr 2022, 12:44 PM
Bob_Li
Bob_Li - avatar
+ 2
Please search the forum using 'BMI' search term to see how others are solving this task 👍
25th Apr 2022, 11:50 AM
Ipang
+ 2
It will always show Underweight or normal. Whatever BMI equal or above 18.5 makes the second if condition true. Hint 1: if your tests go from lower to higher BMI, you just have to test if BMI < next limit. The BMI >= last limit was already tested in previous if. Hint 2: instead of nesting if/else blocks, use elif <next condition>. Will look way cleaner.
25th Apr 2022, 11:54 AM
Emerson Prado
Emerson Prado - avatar
+ 2
I figured It out, thank you
25th Apr 2022, 1:09 PM
Zanele
Zanele - avatar
+ 2
Solo , pls avoid answering with the finished code. The main goal of the app is to help people learn. So giving hints and let people understand and find the solution works best. BTW, also see the 2 hints in my earlier answer.
25th Apr 2022, 3:18 PM
Emerson Prado
Emerson Prado - avatar
+ 2
Emerson Prado, given the fact that Bob_Li has already fixed one condition without paying attention to the previous one, I decided to summarize with this piece of code, especially since I didn’t really like your explanation. Sometimes it’s better to see once than to explain long and tediously, and in my opinion this is just the case, since Zanele has already mastered the logic of this decision in principle. And since Zanele did not pay attention to my answer, then I can delete it. Happy coding. 😉
25th Apr 2022, 8:18 PM
Solo
Solo - avatar
+ 2
Here's the problem: Second if, overrides third and fourth if! If a>1: Print ("a") Elif a>2: Print ("b") "b" never happens. So: If cal <18.5: print ("Underweight") elif cal <= 25: print ("Normal") elif cal <= 30: print ("Overweight") else: print ("Obesity")
26th Apr 2022, 6:54 PM
Ashkan Shakibi
Ashkan Shakibi - avatar
+ 1
Zanele, press on "@"
26th Apr 2022, 2:51 PM
Solo
Solo - avatar
0
I'm new to this, I have no idea how to answer ppl
26th Apr 2022, 2:18 PM
Zanele
Zanele - avatar
- 1
Oloolo
27th Apr 2022, 6:13 AM
Mirzabek Sayfiddinov