Vpython Error: 'float' object has no attribute '_x'

Vpython Error: 'float' object has no attribute '_x'

我正在为弹丸运动编写 Vpython 模拟,并在这一行上不断收到错误('float' 对象没有属性 '_x')(ball.vel.y = ball.vel.y + g *dt) 。我尝试将 ball.vel.y 的值更改为整数并将 g 更改为整数,但出现相同的错误。这是代码

from vpython import *
import math

ball=sphere(radius=0.1, color=color.red, pos=vector(0.1,0.1,0),make_trail=True)
floor=box(pos=vector(0,0,0), length=10, height=0.01, width=0.01)
g= vector(0,-9.8 ,0)
ball.vel=vector(10*cos(43),10*sin(43),0)
dt=0.1
t=0.0

while(ball.pos.y>-0.001):
    rate(100)
    t=t+dt
    ball.pos.x = ball.pos.x + ball.vel.x*dt
    ball.vel.y = ball.vel.y + g*dt
    ball.pos.y = ball.pos.y + ball.vel.y*dt

g 是向量,g*dt 也是,但是ball.vel.y 是标量,不能将向量加到标量上。不幸的是,错误消息不只是说“您不能将矢量添加到标量”。我注意到,如果您反转这两个数量,则错误消息会更容易理解:TypeError: unsupported operand type(s) for +: 'vpython.cyvector.vector' and 'float'