BMI calculation | Sololearn: Learn to code for FREE!

0

BMI calculation

Hi,i'm trying to solve an exercise in python course and i'm having a problem with the code, i must write a code to calculate and print 4 Test Cases of BMI with different value in eight and weight, this is my code: #your code goes here weight = input(floaat()) height = input(floaat()) BMI = float(weight / height **2) if BMI < 18.5: print ("Underweight") else: if BMI >= 18.5 and BMI < 25: print ("Normal") else: if BMI >= 25 and BMI < 30: print ("Overweight") else: if BMI > 30: print ("Obesity") The problem is, the system and the line 5 give me a "TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int' " i tried to change settings (float,int etc) bu the result is the same, why? and how can i solve this? Thank you!

9/29/2021 1:34:23 PM

Luigi Pietragalla

14 Answers

New Answer

+3

Your input returns you a string, what ever you do inside the input or not, so float(input()) is what you should do, this converts the returned string to float It's float not floaat AND ALSO Your BMI calculates wrong you are dividing weight by the square of your height it's obviously wrong, place brackets accordingly, to get the correct value

+3

Brian it was a underweight case hence it will pass, any thing will give underweight

+2

Make corrections to the input lines: weight = float(input()) height = float(input())

+2

I just noticed your weight and height inputs are swapped and indentation is inconsistent. In sum, here are my suggested corrections: #your code goes here weight = float(input()) height = float(input()) BMI = float(weight/height**2) if BMI < 18.5: print ("Underweight") else: if BMI < 25: print ("Normal") else: if BMI < 30: print ("Overweight") else: print ("Obesity") BTW, you could clean up the nested indentations by using elif. I'll leave that as a further exercise for you.

+2

Your order of input is wrong .

+2

weight = float(input()) height = float(input()) this should be the order see what you have coded

+2

did you change your code to what I have said or not

+1

Luigi Pietragalla the syntax in your conditionals are meaningful in human language, but not in Python syntax. You can correct them in any of several ways. if BMI >= 18.5 and BMI < 25: if 18.5 <= BMI < 25: Or, since we already know BMI >= 18.5 because the prior if statement checked BMI < 18.5, just check the next threshold. if BMI < 25: Makes sense? Also note that your first conditional should be corrected to check BMI < 18.5, not BMI <= 18.5. The third conditional should check < 30, not > 30. The fourth conditional should be BMI >= 30, not > 30. That fourth if statement could be removed altogether because the prior statement established that BMI is not < 30.

+1

Luigi Pietragalla are you sure it even passed test case 1? Height and weight inputs are still swapped in the code you posted. EDIT: I tested and found it passes due to a fluke, that it happens to have the same answer. EDIT 2: pay closer attention to the ranges. For instance, you are checking <= 25 but it should be < 25. Review them all.

+1

Luigi Pietragalla I have just done the challenge and this is how I did it: w = float(input()) h = float(input()) bmi = w/(h*h) if bmi < 18.5: print("Underweight") elif bmi >= 18.5 and bmi < 25: print("Normal") elif bmi >= 25 and bmi < 30: print("Overweight") else: print("Obesity")

0

OK that works (the syntax problem with the "floaat" was just a type errore) the really problem now that if-else structure is giving me a syntax errore: #your code goes here height = float(input()) weight = float(input()) BMI = float (height **2 / (weight)) if BMI <= 18.5: print ("Underweight") else: if BMI >= 18.5 and < 25: print ("Normal") else: if BMI >= 25 and > 30: print ("Overweight") else: if BMI >30: print ("Obesity") At the lines 7 and 8 it give me syntax error and i don't know the reason,i'll used the same structure of the lesson,some explanation?

0

OK i corrected but this exercise don't wanna end,actually this is my code: #your code goes here height = float(input()) weight = float(input()) BMI = float (weight / (height ** 2)) if BMI < 18.5: print ("Underweight") else: if 18.5 >= BMI <= 25: print ("Normal") else: if 25 >= BMI <= 30: print ("Overweight") else: print ("Obesity") the problem here is the second test cases give me input like 130 for weight and 1.7 for height, the first test cases is passed but the second gave the result of the first one (underweight) and not Obesity that is the last statement, how i possible?

0

Yes it passed, the formula to have the BMI is this : weight / height². The first test cases give me input as 1.85 in height and 52 in weight, what is the problem here? i'm stupid or the program just can't work?

0

Ok prabha but how can i solve the problem that all test cases passes with "underweight"? The code for me is clear, if the result of BMI is over 18.5 it would pass on the "else" and then resolve all the instance but it didn't work