[英]TypeError: ufunc 'multiply' did not contain a loop with signature matching types dtype('S32') dtype('S32') dtype('S32')
I am new to coding but I am trying to create a really simple program that will basically plot a line. 我是编码的新手,但我正在尝试创建一个非常简单的程序,它基本上会绘制一条线。 The user will input values for v and a then v and a and x will determine y.
用户将输入v和a然后v的值,a和x将确定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.
basically this will represent a parabola where v is velocity, a is acceleration and x is time. 基本上这将代表抛物线,其中v是速度,a是加速度,x是时间。 But, I keep getting this error:
但是,我一直收到这个错误:
TypeError: ufunc 'multiply' did not contain a loop with signature matching types dtype('S32'
) dtype('S32') dtype('S32')
From the documentation of raw_input
: 从
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. 所以会发生的是你尝试将一个字符串与一个浮点数相乘,例如
y="3" * x - 0.5 * "3" *x**2
,这是未定义的。
The easiest way to circumvent this is to cast the input string to float first. 避免这种情况的最简单方法是首先将输入字符串转换为float。
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
, 请注意,如果您使用的是python 3,则需要使用
input
而不是raw_input
,
a = float(input('Acceleration ='))
我最近遇到了这个问题,通过执行以下操作将x的dtype更改为特定的:
x = np.asarray(x, dtype='float64')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.