TypeError: can’t multiply sequence by non-int of type ‘str’

>>> 
Enter muzzle velocity (m/2): 60
Enter angle (degrees): 45
Traceback (most recent call last):
  File "F:/Python31/Lib/idlelib/test", line 9, in <module>
    range()
  File "F:/Python31/Lib/idlelib/test", line 7, in range
    Distance = float(decimal((2*(x*x))((decimal(math.zsin(y)))*(decimal(math.acos(y)))))/2)
TypeError: can't multiply sequence by non-int of type 'str'

I’m only new, so don’t be too harsh if this is really obvious, but why am i getting this error?

You should convert the data you get from console to integers:

x = int(x)
y = int(y)
Distance = float(decimal((2*(x*x))((decimal(math.zsin(y)))*(decimal(math.acos(y)))))/2)

>>> '60' * '60'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can't multiply sequence by non-int of type 'str'

You are trying to multiply two strings together. You must convert the string input from the user to a number using int() or float().

Also, I’m not sure what you’re doing with decimal; it looks like you’re trying to call the module (the type is in the module, decimal.Decimal) but there’s not much point in converting to a Decimal after doing some floating point math and then converting back to a float.

In the future, post the code that causes the problem (and keep the interaction and traceback). But first try and shrink the code as much as possible while making sure it still causes the error. This is an important step in debugging.

You are using raw_input() for getting the input.
Instead use input(). It will return an Int. Make sure that you input only numbers or input() will raise an error (say in case of a string).
Also, it would be nice if you name your variables properly. x and y don’t convey much. (velocity and angle would be so much better)

Leave a Comment