TypeError:不支持的操作数类型 - :'str'和'str'?

时间:2016-08-24 00:06:27

标签: python python-3.x

我是编程新手,无法弄清楚如何修复此错误:

href="#/"

我的代码:

Traceback (most recent call last):
  File "/Users/aubreyoleary/Documents/Cashier.py", line 31, in <module>
    changePennies = int((amountReceived - amountDue) * 100)
TypeError: unsupported operand type(s) for -: 'str' and 'str'

2 个答案:

答案 0 :(得分:1)

这意味着&#39; 6&#39; - &#39; 4&#39;不会工作,因为他们都是字符串。首先需要将字符串值转换为数字:

changePennies = int(round((float(amountReceived) - float(amountDue)) * 100, 0))

答案 1 :(得分:0)

这是因为amountReceivedamountDue的数据类型是字符串。在执行arithematic float操作之前,必须将其强制转换为-

使用:

而不是int((amountReceived - amountDue) * 100)
changePennies = int(float(amountReceived) - float(amountDue)) * 100
相关问题