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

2024-04-29 16:00:05 发布

您现在位置:Python中文网/ 问答频道 /正文

这里的问题是什么?你知道吗

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)

皮查姆喊道:

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

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

然而,它在ipython下工作得很好。水处理设施


Tags: ininputvaluethreadbagspriceprintcost
1条回答
网友
1楼 · 发布于 2024-04-29 16:00:05

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)

相关问题 更多 >