How to multiply sales tax

how to multiply sales tax

TypeError: can't multiply sequence by non-int of type 'float'

order by

It would help if you provide the back-trace for the error. I ran your code, and got this back-trace:

The answer is that this is a precedence problem. If you just did this:

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:

The solution is to add parentheses to force the precedence you want:

Here is documentation of operator precedence in Python.

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:

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:

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

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.


Category: Taxes

Similar articles: