TypeError: unsupported operand type(s) for /: 'str' and 'float'

孤者浪人 提交于 2021-01-29 04:48:13

问题


I am using Python 2 but I'm getting an error:

TypeError: unsupported operand type(s) for /: 'str' and 'float'

when the script runs to the last line. I don't understand which variable is still a string type.

from sys import argv
script, age, height=argv
print 'you\'re %r old'%age
weight=input('and i need your weight to calculate BMI, can you tell me:') 
print 'your BMI is %r'%weight/((float(height)/100)**2)

回答1:


from sys import argv
script, age, height=argv
print 'you\'re %r old'%age
weight=input('and i need your weight to calculate BMI, can you tell me:') 
print 'your BMI is %r'%(weight/((float(height)/100)**2))

i found the solution, it's because a formula after % must be in ()




回答2:


weight = float(weight)
height = float(height)
age = int(age)

You forgot to convert the input from string to numeric. To diagnose:

print weight, type(weight)
print height, type(height)
print age, type(age)
...


来源:https://stackoverflow.com/questions/43106154/typeerror-unsupported-operand-types-for-str-and-float

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!