Python:浮点数的简单计算中不支持的操作数'str'和'float'

时间:2016-02-18 15:31:33

标签: python string

这里似乎有什么问题?

THREAD = 2.55

bags_value = input('Bags value in gold: ')
cloth_price = input('Cloth price in gold: ')
dust_price = input('Dust price in gold: ')

bags_cost = (2 * THREAD) + 6 * ((2 * dust_price) + 2 * (5 * cloth_price))

profit = bags_value - bags_cost
print(profit)

PyCharm喊道:

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

如果我从终端执行脚本,也会发生同样的情况。

然而,它在ipython下完美运行。 WTF

1 个答案:

答案 0 :(得分:2)

input返回string。你必须转向float

THREAD = 2.55

bags_value = input('Bags value in gold: ')
cloth_price = input('Cloth price in gold: ')
dust_price = input('Dust price in gold: ')

bags_cost = (2 * THREAD) + 6 * ((2 * float(dust_price)) + 2 * (5 * float(cloth_price)))

profit = bags_value - bags_cost
print(profit)