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')
4 Respostas
+ 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')
+ 1
Ohhhh thx a lot ! I knew that it was this type of problem but couldn't figure it out !! Thxxx
+ 1
https://www.sololearn.com/discuss/2684939/?ref=app
https://www.sololearn.com/discuss/2686254/?ref=app
https://www.sololearn.com/discuss/2750158/?ref=app
https://www.sololearn.com/discuss/2795746/?ref=app
https://www.sololearn.com/discuss/2744385/?ref=app
https://www.sololearn.com/discuss/2811456/?ref=app
https://www.sololearn.com/discuss/1408478/?ref=app
https://www.sololearn.com/discuss/2787366/?ref=app
https://www.sololearn.com/discuss/2777286/?ref=app
https://www.sololearn.com/discuss/2771623/?ref=app
https://www.sololearn.com/discuss/2687547/?ref=app
https://www.sololearn.com/discuss/2686010/?ref=app
https://www.sololearn.com/discuss/2748154/?ref=app
https://www.sololearn.com/discuss/2686011/?ref=app
https://www.sololearn.com/discuss/2776665/?ref=app
https://www.sololearn.com/discuss/2806015/?ref=app
https://www.sololearn.com/discuss/2788765/?ref=app
https://www.sololearn.com/discuss/2751953/?ref=app
https://www.sololearn.com/discuss/2686226/?ref=app
https://www.sololearn.com/discuss/2765814/?ref=app
http
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')