我是一個新手,Python和最近已試圖創建一個BMI計算器,但我用下面的代碼有錯誤:的Python:的raw_input和不支持的操作類型
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'
我這個愚蠢的問題和錯誤纏身代碼道歉,但我很新的編程並欣賞任何形式的幫助。 :)
你從'raw_input'得到的東西總是在字符串類型中用int() ' – The6thSense
請注意,'weight.isdigit'只引用方法,它實際上不會調用*方法。 –
你的程序中有很多錯誤只是通過python的基礎,然後再試一次,你在這裏試圖完成什麼'如果高度==退出:' – The6thSense