TypeError: can't multiply sequence by non-int of type 'float'
I am a noob to Python and have not had any luck figuring this out. I want to be able to keep the tax variable in the code so it would be easily updated should it change. I have experimented with different means but was only able to get it to skip the print tax line and print the same values for the total and subtotal. How do I multiply the tax variable by sum(items_count)? Here is the code:
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_coun开发者_JS百科t) * tax
total = (sum(items_count) * tax) + sum(items_count)
print 'The total of your items is: ' '$%.2f' % total
It would help if you provide the back-trace for the error. I ran your code, and got this back-trace:
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'
The answer is that this is a precedence problem. If you just did this:
sum(items_count) * tax
it would work, but because you have the expression with the string and the %
operator, the call to sum()
is tied to the string, and effectively you have:
<string_value> * tax
The solution is to add parentheses to force the precedence you want:
print 'The amount of sales tax is: ' '$%.2f' % (sum(items_count) * tax)
Here is documentation of operator precedence in Python.
http://docs.python.org/reference/expressions.html#summary
Note that %
has the same precedence as *
, so the order is then controlled by the left-to-right rule. Thus, the string and the call to sum()
are connected with the %
operator, and you are left with <string_value> * tax
.
Note that instead of parentheses, you could also use an explicit temporary:
items_tax = sum(items_count) * tax
print 'The amount of sales tax is: ' '$%.2f' % items_tax
When you aren't sure what is going on, sometimes it's a good idea to start using explicit temporary variables, and check to see that each one is set to the value you were expecting.
P.S. You don't actually need all the calls to float()
. The value 0.06
is already a float value, so it is sufficient to just say:
tax = 0.06
I like to put the initial zero on fractions, but you can use either of tax = 0.06
or tax = .06
, it doesn't matter.
I like how you convert the prices to float by wrapping the raw_input()
call in float()
. I suggest that you should do the same thing for count
, wrap the raw_input()
call in int()
to get an int
value. Then the later expression can simply be
count -= 1
It's a bit tricky that count
is initially set to a string and then re-bound later. If a silly or crazy user enters an invalid count, int()
will raise an exception; it is better if the exception happens right away, right on the call to raw_input()
, rather than later in a seemingly simple expression.
And of course you aren't using y
for anything in your code sample.
You need to use
'$%.2f' % (sum(items_count) * tax)
instead of
'$%.2f' % sum(items_count) * tax
The one you used will be evaluated as ('$%.2f' % sum(items_count)) * tax
, which is an error (multiplying a string by a float).
You need parentheses around the sum(items_count) * tax
.
I took the liberty of cleaning up your code a bit as well :)
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))
Just add parens:
print 'The amount of sales tax is: ' '$%.2f' % (sum(items_count) * tax)
精彩评论