为什么会出现TypeError:无法将序列乘以'float'类型的非整数?

发布于 2021-01-29 15:05:58

我输入要获取的销售金额(按输入)乘以定义的营业税(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'
关注者
0
被浏览
61
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

    raw_input返回一个字符串(一个字符序列)。在Python中,将字符串和浮点数相乘并没有定义的含义(而将字符串和整数相乘则具有以下含义:"AB" * 3is "ABABAB";多少是"L" * 3.14多少?请不要回复"LLL|")。您需要将字符串解析为数字值。

    您可能要尝试:

    salesAmount = float(raw_input("Insert sale amount here\n"))
    


知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看