为什么我会得到 TypeError:不能将序列乘以“float"类型的非整数? [英] Why do I get TypeError: can't multiply sequence by non-int of type 'float'?
问题描述
我正在输入以获取要乘以定义的销售税 (0.08) 的销售金额(按输入),然后打印总金额(销售税乘以销售金额).
I am typing to get a sale amount (by input) to be multiplied by a defined sales tax (0.08) and then have it print the total amount (sales tax times sale amount).
我遇到了这个错误.任何人都知道可能有什么问题或有任何建议?
I run into this error. Anyone know what could be wrong or have any suggestions?
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 中,字符串和浮点数相乘没有定义的含义(而字符串和整数相乘的含义是:"AB" * 3
是 "ABABAB"
;如何"L" * 3.14
多少?请不要回复"LLL|"
).您需要将字符串解析为数值.
raw_input
returns a string (a sequence of characters). In Python, multiplying a string and a float makes no defined meaning (while multiplying a string and an integer has a meaning: "AB" * 3
is "ABABAB"
; how much is "L" * 3.14
? Please do not reply "LLL|"
). You need to parse the string to a numerical value.
您可能想尝试:
salesAmount = float(raw_input("Insert sale amount here\n"))
这篇关于为什么我会得到 TypeError:不能将序列乘以“float"类型的非整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!