Why do I get "ufunc 'multiply' did not contain a loop with signature matching types dtype('S32') dtype('S32') dtype('S32')" with values from raw_input

94,158

Solution 1

From the documentation of raw_input:

The function then reads a line from input, converts it to a string (stripping a trailing newline), and returns that.

So what happens is that you try to multiply a string with a float, something like y="3" * x - 0.5 * "3" *x**2, which is not defined.

The easiest way to circumvent this is to cast the input string to float first.

x = np.linspace(0., 9., 10)
a = float(raw_input('Acceleration ='))
v = float(raw_input('Velocity = '))
y = v * x - 0.5 * a * x**2

Mind that if you're using Python 3, you'd need to use input instead of raw_input,

a = float(input('Acceleration ='))

Solution 2

I faced this problem recently, change the dtype of x to something specific by doing:

x = np.asarray(x, dtype='float64')
Share:
94,158

Related videos on Youtube

A. Robinson
Author by

A. Robinson

Updated on July 18, 2022

Comments

  • A. Robinson
    A. Robinson almost 2 years

    I am trying to create a really simple program that will plot a plot a parabola where v is velocity, a is acceleration and x is time. The user will input values for v and a, then v and a and x will determine y.

    I attempted to do this with this:

    x = np.linspace(0., 9., 10)
    a = raw_input('Acceleration =')
    v = raw_input('Velocity = ')
    y = v * x - 0.5 * a * x**2.
    

    But, I keep getting this error:

    TypeError: ufunc 'multiply' did not contain a loop with signature matching types dtype('S32') dtype('S32') dtype('S32')

    What does this mean?

  • AsheKetchum
    AsheKetchum about 7 years
    why is that the easiest way?
  • ImportanceOfBeingErnest
    ImportanceOfBeingErnest about 7 years
    Well, for python2 you can use input instead of raw_input but that hides a bit the origin of the problem. And since input evaluates the input, there are all kind of hidden traps, while float(raw_input()) either works or throws an error.
  • AsheKetchum
    AsheKetchum about 7 years
    so casting to float is the safest way?
  • ImportanceOfBeingErnest
    ImportanceOfBeingErnest about 7 years
    Safe in the sense that it may fail and throw an error, but you will always know why it fails (because you haven't typed in a number). For beginners it's definitely the most understandable way.