[英]Vpython Error: 'float' object has no attribute '_x'
I am writing a Vpython simulation for projectile motion and keep getting the error ('float' object has no attribute '_x') on this line(ball.vel.y = ball.vel.y + g*dt).我正在为射弹运动编写 Vpython 模拟,并在此行 (ball.vel.y = ball.vel.y + g*dt) 上不断收到错误消息(“浮动”object 没有属性“_x”)。 I have tried changing the values of ball.vel.y to an integer and changing g to an integer but the same error occurs.
我尝试将 ball.vel.y 的值更改为 integer 并将 g 更改为 integer 但出现相同的错误。 Here is the code
这是代码
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 is a vector, as is g*dt, but ball.vel.y is a scalar, and you can't add a vector to a scalar. g 是矢量,g*dt 也是,但 ball.vel.y 是标量,不能将矢量与标量相加。 It's unfortunate that the error message doesn't just say "You can't add a vector to a scalar".
不幸的是,错误消息不只是说“您不能将矢量添加到标量”。 I note that if you reverse the two quantities the error message is a bit more understandable: TypeError: unsupported operand type(s) for +: 'vpython.cyvector.vector' and 'float'
我注意到,如果您反转这两个数量,则错误消息更容易理解:TypeError: unsupported operand type(s) for +: 'vpython.cyvector.vector' and 'float'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.