Python TypeError:无法将序列与“float”类型的非int相乘

Python TypeError:无法将序列与“float”类型的非int相乘,python,Python,我是Python的高手,还没有找到解决这个问题的办法。我希望能够在代码中保留tax变量,以便在它发生更改时可以轻松地进行更新。我尝试过不同的方法,但只能让它跳过打印税行,打印相同的合计和小计值。如何将税务变量乘以SUMU计数?代码如下: items_count = [] tax = float(.06) y = 0 count = raw_input('How many items do you have? ') while count > 0: price = floa

我是Python的高手,还没有找到解决这个问题的办法。我希望能够在代码中保留tax变量,以便在它发生更改时可以轻松地进行更新。我尝试过不同的方法,但只能让它跳过打印税行,打印相同的合计和小计值。如何将税务变量乘以SUMU计数?代码如下:

   items_count = []
tax = float(.06)
y = 0

count = raw_input('How many items do you have? ')

while count > 0:
    price = float(raw_input('Please enter the price of your item: '))
    items_count.append(price)
    count = int(count) - 1

print 'The subtotal of your items is: ' '$%.2f' % sum(items_count)
print 'The amount of sales tax is: ' '$%.2f' % sum(items_count) * tax
total = (sum(items_count) * tax) + sum(items_count)
print 'The total of your items is: ' '$%.2f' % total
你需要使用

'$%.2f' % (sum(items_count) * tax)
而不是

'$%.2f' % sum(items_count) * tax
您使用的将被评估为“$%.2f”%sumitems\u count*tax,这是一个将字符串乘以浮点数的错误。

您需要使用

'$%.2f' % (sum(items_count) * tax)
而不是

'$%.2f' % sum(items_count) * tax
您使用的将被评估为“$%.2f”%sumitems\u count*tax,这是一个将字符串乘以浮点数的错误。

只需添加参数:

print 'The amount of sales tax is: ' '$%.2f' % (sum(items_count) * tax)
只需添加parens:

print 'The amount of sales tax is: ' '$%.2f' % (sum(items_count) * tax)

您需要在sumitems_count*税的周围加上括号

我还冒昧地清理了一下您的代码:

items_count = []
tax = float(.06)

count = int(raw_input('How many items do you have? '))

while count:
    price = float(raw_input('Please enter the price of your item: '))
    items_count.append(price)
    count -= 1

print 'The subtotal of your items is: $%.2f' % sum(items_count)
print 'The amount of sales tax is: $%.2f' % (sum(items_count) * tax)
print 'The total of your items is: $%.2f' % ((sum(items_count) * tax) +
        sum(items_count))

您需要在sumitems_count*税的周围加上括号

我还冒昧地清理了一下您的代码:

items_count = []
tax = float(.06)

count = int(raw_input('How many items do you have? '))

while count:
    price = float(raw_input('Please enter the price of your item: '))
    items_count.append(price)
    count -= 1

print 'The subtotal of your items is: $%.2f' % sum(items_count)
print 'The amount of sales tax is: $%.2f' % (sum(items_count) * tax)
print 'The total of your items is: $%.2f' % ((sum(items_count) * tax) +
        sum(items_count))

如果您提供错误的回溯跟踪,这将有所帮助。我运行了你的代码,得到了以下跟踪:

Traceback (most recent call last):
  File "t.py", line 13, in <module>
    print 'The amount of sales tax is: ' '$%.2f' % sum(items_count) * tax
TypeError: can't multiply sequence by non-int of type 'float'
它可以工作,但因为您有一个带有字符串和%运算符的表达式,所以对sum的调用与字符串绑定,实际上您有:

<string_value> * tax
下面是Python中运算符优先级的文档

请注意,%的优先级与*,因此顺序由从左到右规则控制。因此,字符串和sum调用与%运算符连接,剩下的是*tax

请注意,您也可以使用显式的临时符号,而不是括号:

items_tax = sum(items_count) * tax
print 'The amount of sales tax is: ' '$%.2f' % items_tax
当您不确定发生了什么时,有时最好开始使用显式临时变量,并检查每个变量是否设置为您期望的值

顺便说一句,你实际上不需要所有的电话来浮动。值0.06已经是浮点值,因此只需说:

tax = 0.06
我喜欢把分数的初始值设为零,但是你可以使用tax=0.06或tax=0.06,这都没关系

我喜欢通过将原始输入调用包装为float将价格转换为float的方式。我建议您对count执行相同的操作,将原始输入调用包装为int以获得int值。那么后面的表达式可以是

count -= 1
count最初设置为字符串,然后在以后重新绑定,这有点棘手。如果愚蠢或疯狂的用户输入无效计数,int将引发异常;最好是在调用原始输入时立即发生异常,而不是稍后在一个看似简单的表达式中发生异常


当然,您的代码示例中没有使用y来表示任何内容。

如果您提供错误的回溯跟踪,则会有所帮助。我运行了你的代码,得到了以下跟踪:

Traceback (most recent call last):
  File "t.py", line 13, in <module>
    print 'The amount of sales tax is: ' '$%.2f' % sum(items_count) * tax
TypeError: can't multiply sequence by non-int of type 'float'
它可以工作,但因为您有一个带有字符串和%运算符的表达式,所以对sum的调用与字符串绑定,实际上您有:

<string_value> * tax
下面是Python中运算符优先级的文档

请注意,%的优先级与*,因此顺序由从左到右规则控制。因此,字符串和sum调用与%运算符连接,剩下的是*tax

请注意,您也可以使用显式的临时符号,而不是括号:

items_tax = sum(items_count) * tax
print 'The amount of sales tax is: ' '$%.2f' % items_tax
当您不确定发生了什么时,有时最好开始使用显式临时变量,并检查每个变量是否设置为您期望的值

顺便说一句,你实际上不需要所有的电话来浮动。值0.06已经是浮点值,因此只需说:

tax = 0.06
我喜欢把分数的初始值设为零,但是你可以使用tax=0.06或tax=0.06,这都没关系

我喜欢通过将原始输入调用包装为float将价格转换为float的方式。我建议您对count执行相同的操作,将原始输入调用包装为int以获得int值。那么后面的表达式可以是

count -= 1
count最初设置为字符串,然后在以后重新绑定,这有点棘手。如果愚蠢或疯狂的用户输入无效计数,int将引发异常;最好是在调用原始输入时立即发生异常,而不是稍后在一个看似简单的表达式中发生异常


当然,代码示例中的任何内容都没有使用y。

请重新粘贴代码,使其完全格式化为代码。目前无法阅读。谢谢谢谢大家回答我的问题@steveha,很好的解释,是的,在我第一次编写代码时,y变量实际上是有意义的@Kimvais,谢谢你为我清理代码。请你重新粘贴你的代码,使其完全格式化为代码。目前无法阅读。谢谢谢谢大家回答我的问题@史蒂文哈,很好的解释 是的,在我第一次写代码的时候,y变量实际上是有意义的@Kimvais,谢谢你帮我清理代码。