类型错误:ufunc 'multiply' 不包含签名匹配类型 dtype('S32') dtype('S32') dtype('S32') 的循环 [英] TypeError: ufunc 'multiply' did not contain a loop with signature matching types dtype('S32') dtype('S32') dtype('S32')
问题描述
我是编码新手,但我正在尝试创建一个非常简单的程序,该程序基本上可以绘制一条线.用户将输入 v 和 a 的值,然后 v 和 a 和 x 将确定 y.我试图这样做:
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. 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.
基本上这将表示一条抛物线,其中 v 是速度,a 是加速度,x 是时间.但是,我不断收到此错误:
basically this will represent a parabola where v is velocity, a is acceleration and x is time. But, I keep getting this error:
TypeError: ufunc 'multiply' did not contain a loop with signature matching types dtype('S32'
) dtype('S32') dtype('S32')
推荐答案
来自 raw_input
的文档:
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.
所以会发生的情况是,您尝试将字符串与浮点数相乘,例如 y="3" * x - 0.5 * "3" *x**2
,但未定义.
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
请注意,如果您使用的是 python 3,则需要使用 input
而不是 raw_input
,
Mind that if you're using python 3, you'd need to use input
instead of raw_input
,
a = float(input('Acceleration ='))
这篇关于类型错误:ufunc 'multiply' 不包含签名匹配类型 dtype('S32') dtype('S32') dtype('S32') 的循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!