BMI code not working | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

BMI code not working

I don't understand why this code isn't working: I want to do the BMI exercice but the system says an error on line 4. Weight = input() Height = input() BMI = Weight / (Height**2) if BMI < 18.5: print('Underweight') elif BMI < 25: print('Normal') elif BMI < 30: print('Overweight') else: print('Obesity')

28th Jun 2021, 8:32 AM
Matteo Bonaldi
Matteo Bonaldi - avatar
4 Answers
+ 1
because you have a variable of type str. cannot be divided, use a variable of type str, it is better to make the type of the variable "float". You have an input of type "str". Variables of type str cannot divided. Possible only with types: int, float: Weight = float (input ()) Height = float (input ()) BMI = weight / (height ** 2) if BMI <18.5: print ('Underweight') elif BMI <25: print ('Normal') elif BMI <30: print ('Redundant') yet: print ('Obesity')
28th Jun 2021, 8:37 AM
SammE
SammE - avatar
+ 1
Ohhhh thx a lot ! I knew that it was this type of problem but couldn't figure it out !! Thxxx
28th Jun 2021, 8:38 AM
Matteo Bonaldi
Matteo Bonaldi - avatar
0
Matteo Bonaldi input() returns a string. So , your code is not working correctly. To get a input as a float , use float() function. And in your control structures conditions are wrong. They are, (BMI<18.5) - 'Underweight' (BMI>=18.5 and BMI <25) - ' Normal' (BMI >=25 and BMI<30) -'Overweight' (BMI >=30) - ' Obesity ' correct code↓ Weight = float(input()) Height =float (input()) BMI = Weight / (Height**2) 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')
28th Jun 2021, 8:44 AM
˜”*°•.˜”*°• Mohan 333 •°*”˜.•°*”˜
˜”*°•.˜”*°• Mohan 333 •°*”˜.•°*”˜ - avatar