TypeError:/:' str'不支持的操作数类型并且'漂浮'

时间:2017-03-29 23:53:20

标签: python python-2.7

我正在使用Python 2,但我收到了错误:

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

当脚本运行到最后一行时。我不明白哪个变量仍然是字符串类型。

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)

2 个答案:

答案 0 :(得分:2)

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 :(得分:0)

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

您忘记将输入从字符串转换为数字。诊断:

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