views:

4177

answers:

4
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'm a learning Python programmer. 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? Thank you.

+2  A: 

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!

Eric Palakovich Carr
Thanks much bro. I appreciate the help! I didn't know about being able to store a float value when defining. I was trying to do it on the fly (but I was trying to change the value from a string to an int - so I was will in the wrong since I needed a float value).
SD
+5  A: 

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"))
Marc Gravell
-1: all the apologizing. Download python. Try it. What did you learn?
S.Lott
I learned to read the syntax error before posting here and using Google. I was trying to convert the raw_input for salesAmount to an int rather than a float. Do you know why int will not work, but rather float would? Bare with me, lol.
SD
Multiplying (using the * operator) a string by an integer is well-defined in Python. Multiplying a string by a float is not defined, and is what Python was complaining about.
Greg Hewgill
Thanks Greg! I guess I need to get down some more basic rules.
SD
@Raj: I said it was well-defined, but it's not necessarily what you expect. "abc" * 3 is "abcabcabc", and therefore "5" * 3 is "555".
Greg Hewgill
A: 

Big thanks to the both of you. Noob-programmer's mistake not making the raw_input as a float value.

Thanks for the help, again.

SD
this should be a comment, not an answer. Please delete this answer and add a comment instead.
nosklo
A: 

I'm trying to do

p = T * 2 * dotxmu * pi

were " dotxmu " has a componet consisting of three elements ( i.e. " 0,0,0).

I keep getting the message

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

What should I do?

da fermionator