这里似乎有什么问题?
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
答案 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)