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

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

我输入要获取的销售金额(按输入)乘以定义的营业税(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'

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'

回答 0

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

您可能要尝试:

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

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.

You might want to try:

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

回答 1

也许这会在将来对其他人有所帮助-尝试使用多个float和float列表时遇到了相同的错误。关键是这里的每个人都在谈论将浮点数与字符串相乘(但是这里所有元素一直都是浮点数),因此问题实际上是在列表上使用*运算符。

例如:

import math
import numpy as np
alpha = 0.2 
beta=1-alpha
C = (-math.log(1-beta))/alpha

coff = [0.0,0.01,0.0,0.35,0.98,0.001,0.0]
coff *= C

错误:

    coff *= C 
TypeError: can't multiply sequence by non-int of type 'float'

解决方案-将列表转换为numpy数组:

coff = np.asarray(coff) * C

Maybe this will help others in the future – I had the same error while trying to multiple a float and a list of floats. The thing is that everyone here talked about multiplying a float with a string (but here all my element were floats all along) so the problem was actually using the * operator on a list.

For example:

import math
import numpy as np
alpha = 0.2 
beta=1-alpha
C = (-math.log(1-beta))/alpha

coff = [0.0,0.01,0.0,0.35,0.98,0.001,0.0]
coff *= C

The error:

    coff *= C 
TypeError: can't multiply sequence by non-int of type 'float'

The solution – convert the list to numpy array:

coff = np.asarray(coff) * C

回答 2

问题是salesAmount设置为字符串。如果您在python解释器中输入变量,然后按Enter,您将看到输入的值用引号引起来。例如,如果输入56.95,您将看到:

>>> sales_amount = raw_input("[Insert sale amount]: ")
[Insert sale amount]: 56.95
>>> sales_amount
'56.95'

您需要先将字符串转换为浮点数,然后再乘以营业税。我会留给您解决。祝好运!

The problem is that salesAmount is being set to a string. If you enter the variable in the python interpreter and hit enter, you’ll see the value entered surrounded by quotes. For example, if you entered 56.95 you’d see:

>>> sales_amount = raw_input("[Insert sale amount]: ")
[Insert sale amount]: 56.95
>>> sales_amount
'56.95'

You’ll want to convert the string into a float before multiplying it by sales tax. I’ll leave that for you to figure out. Good luck!


回答 3

您不能将字符串和浮点数相乘,而是尝试如下操作。

totalAmount = salesAmount * float(salesTax)

You can’t multiply string and float.instead of you try as below.it works fine

totalAmount = salesAmount * float(salesTax)