gpt4 book ai didi

python - 为什么我得到 "ufunc ' multiply' did not contain a loop with signature matching types dtype ('S32' ) dtype ('S32' ) dtype ('S32' )"with values from raw_input

转载 作者:太空狗 更新时间:2023-10-29 17:18:28 27 4
gpt4 key购买 nike

我正在尝试创建一个非常简单的程序,它将绘制一条抛物线图,其中 v 是速度,a 是加速度,x是时候了。用户将输入 va 的值,然后是 va 以及 x 将确定 y

我试图用这个来做到这一点:

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

但是,我一直收到这个错误:

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

这是什么意思?

最佳答案

来自documentation of raw_input :

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

那么当您尝试将一个字符串与一个 float 相乘时,会发生什么,例如 y="3"* x - 0.5 * "3"*x**2,它没有定义.

避免这种情况的最简单方法是首先将输入字符串转换为 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

请注意,如果您使用的是 Python 3,则需要使用 input 而不是 raw_input

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

关于python - 为什么我得到 "ufunc ' multiply' did not contain a loop with signature matching types dtype ('S32' ) dtype ('S32' ) dtype ('S32' )"with values from raw_input,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42013903/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com