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!

29th Sep 2021, 1:34 PM
Luigi Pietragalla
Luigi Pietragalla - avatar
14 Answers
+ 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
29th Sep 2021, 1:44 PM
Prabhas Koya
+ 3
Brian it was a underweight case hence it will pass, any thing will give underweight
1st Oct 2021, 1:02 PM
Prabhas Koya
+ 2
Make corrections to the input lines: weight = float(input()) height = float(input())
29th Sep 2021, 1:44 PM
Brian
Brian - avatar
+ 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.
30th Sep 2021, 3:31 AM
Brian
Brian - avatar
+ 2
Your order of input is wrong .
1st Oct 2021, 12:59 PM
Prabhas Koya
+ 2
weight = float(input()) height = float(input()) this should be the order see what you have coded
1st Oct 2021, 1:24 PM
Prabhas Koya
+ 2
did you change your code to what I have said or not
1st Oct 2021, 1:57 PM
Prabhas Koya
+ 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.
29th Sep 2021, 9:34 PM
Brian
Brian - avatar
+ 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.
1st Oct 2021, 1:00 PM
Brian
Brian - avatar
+ 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")
1st Oct 2021, 2:09 PM
Asanda Ndimande
Asanda Ndimande - avatar
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?
29th Sep 2021, 8:43 PM
Luigi Pietragalla
Luigi Pietragalla - avatar
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?
1st Oct 2021, 12:48 PM
Luigi Pietragalla
Luigi Pietragalla - avatar
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?
1st Oct 2021, 1:17 PM
Luigi Pietragalla
Luigi Pietragalla - avatar
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
1st Oct 2021, 1:40 PM
Luigi Pietragalla
Luigi Pietragalla - avatar