我是 Python 的新手,最近一直在尝试创建一个 BMI 计算器,但我遇到以下代码错误:
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):')
每当我尝试执行代码时,我都可以输入体重和身高,但我会遇到以下错误消息:
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'
对于这个愚蠢的问题和错误缠身的代码,我深表歉意,但我对编程非常陌生,并感谢任何形式的帮助。:)