I am a newbie to Python and have been recently attempting to create a BMI calculator, but I am having errors with the following code:
def calculator():
weight = raw_input('Please enter your weight (kg):')
if weight.isdigit and weight > 0:
height = raw_input('Please enter your height (m):')
if height.isdigit and height > 0:
bmi = (weight) / (height ** 2)
print "Your BMI is", bmi
if bmi < 18.5:
print 'You are underweight.'
if bmi >= 18.5 and bmi < 25:
print 'Your BMI is normal.'
if bmi >= 25 and bmi < 30:
print 'You are overweight.'
if bmi >= 30:
print 'You are obese.'
else:
height = raw_input('Please state a valid number (m):')
else:
weight = raw_input('Please state a valid number (kg):')
Whenever I try to execute the code, I am able to enter weight and height, but I am then confronted with this error message:
Traceback (most recent call last):
File "*location*", line 40, in <module>
calculator()
File "*location*", line 15, in calculator
bmi = (weight) / (height ** 2)
TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int'
I apologize for this dumb question and error-ridden code, but I am very new to programming and appreciate any kind of help. :)
raw_inputare always in string type convert them to int usingint()- The6thSenseweight.isdigitonly references the method, it doesn't actually call that method. - Martijn Pieters♦if height == exit:- The6thSense