M.Love
M.Love

Reputation: 1

How do I resolve "'<' not supported between instances of 'int' and 'NoneType'" in python 3?

I'd like to put a condition in code below. As you can see, I want to print the respective message for respective values of BMI. I am having an error for <= or >+ used in the if and elif statements. Please tell me what to use in this case.

def bmi(w,h):
    print((w)/(h**2))

weight = float(input('Weight (kg): '))
height = float(input('Height (m): '))
b = bmi(weight,height)

if 18<b<25 :
    print('Your weight is normal.')
elif b<18:
    print('You are underweight.')
elif 25<b<29:
    print('You are overweight.')
elif b>29:
    print('You are obese.')

Output -

Weight (kg): 65
Height (m): 1.72
21.971335857220122
Traceback (most recent call last):
  File "D:/Coding/Python Exercises/Ass5/Ass5.py", line 8, in <module>
    if 18<b<25 :
TypeError: '<' not supported between instances of 'int' and 'NoneType'

Process finished with exit code 1

Upvotes: 1

Views: 7470

Answers (1)

enumaris
enumaris

Reputation: 1938

The problem lies in your function:

def bmi(w,h):
    print((w)/(h**2))

You don't return anything in this function and so, when you are calling it in the line b = bmi(weight,height), you don't set b to be anything other than a None value. All you've done is print the bmi to the screen, but you haven't set b to a float value which is why you get the error (which is telling you you can't compare a float to a None using operators like >). You want to add a return statement so that your function actually returns something when called, like so:

def bmi(w,h):
    print((w)/(h**2))
    return w/h**2

Upvotes: 2

Related Questions