【问题标题】:TypeError: can't multiply sequence by non-int of type 'float'TypeError:不能将序列乘以“float”类型的非整数
【发布时间】:2010-12-30 21:26:30
【问题描述】:

我是 Python 的菜鸟,我没有任何运气来解决这个问题。我希望能够将税收变量保留在代码中,以便在更改时轻松更新。我尝试了不同的方法,但只能让它跳过打印税行并为总计和小计打印相同的值。如何将税收变量乘以 sum(items_count)?代码如下:

   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

【问题讨论】:

  • 能否请您重新粘贴您的代码,使其完全格式化为代码。暂时无法阅读。谢谢!
  • 感谢大家回答我的问题。 @steveha,很好的解释,是的,我第一次编写代码时 y 变量实际上是有意义的。 @Kimvais,感谢您为我清理代码。

标签: python


【解决方案1】:

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

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(items_count) * tax

它会起作用,但是因为你有带有字符串的表达式和 % 运算符,所以对 sum() 的调用与字符串相关联,实际上你有:

<string_value> * tax

解决办法是加括号强制你想要的优先级:

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

这里是 Python 中运算符优先级的文档。

http://docs.python.org/reference/expressions.html#summary

请注意%* 具有相同的优先级,因此顺序由从左到右的规则控制。因此,字符串和对sum() 的调用与% 运算符相连,而您只剩下&lt;string_value&gt; * tax

请注意,您也可以使用显式临时代替括号:

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

当您不确定发生了什么时,有时最好开始使用显式临时变量,并检查每个变量是否都设置为您期望的值。

附:您实际上并不需要对float() 的所有调用。 0.06 的值已经是一个浮点值,所以只要说:

tax = 0.06

我喜欢在分数上加上初始零,但你可以使用tax = 0.06tax = .06,没关系。

我喜欢您通过将raw_input() 调用包装在float() 中来将价格转换为浮动价格。我建议你应该对count 做同样的事情,将raw_input() 调用包装在int() 中以获得int 值。那么后面的表达式可以简单地是

count -= 1

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

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

【讨论】:

    【解决方案2】:

    sum(items_count) * tax 周围需要括号。

    我也冒昧地清理了你的代码 :)

    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))
    

    【讨论】:

      【解决方案3】:

      只需添加括号:

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

      【讨论】:

        【解决方案4】:

        你需要使用

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

        而不是

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

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

        【讨论】:

          猜你喜欢
          • 2012-10-09
          • 1970-01-01
          • 2017-02-04
          • 2019-03-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-12-09
          • 1970-01-01
          相关资源
          最近更新 更多