我正在输入一个销售金额(通过输入)乘以定义的销售税 (0.08),然后让它打印总金额(销售税乘以销售金额)。
我遇到了这个错误。任何人都知道可能出了什么问题或有任何建议吗?
salesAmount = raw_input (["Insert sale amount here \n"])
['Insert sale amount here \n']20.99
>>> salesTax = 0.08
>>> totalAmount = salesAmount * salesTax
Traceback (most recent call last):
File "<pyshell#57>", line 1, in <module>
totalAmount = salesAmount * salesTax
TypeError: can't multiply sequence by non-int of type 'float'
最佳答案
raw_input
返回一个字符串(字符序列)。在 Python 中,字符串和 float 相乘没有明确的含义(而字符串和整数相乘有含义:"AB"* 3
is "ABABAB"
; 如何"L"* 3.14
是多少?请不要回复 "LLL|"
)。您需要将字符串解析为数值。
你可能想试试:
salesAmount = float(raw_input("Insert sale amount here\n"))
关于python - 为什么我会收到 TypeError : can't multiply sequence by non-int of type 'float' ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/485789/