# Python Ship Movement Speed Function Question

Asked 2 weeks ago, Updated 2 weeks ago, 2 views

Questions

If you put an array of time, x, and y as a function factor, write a function (function name: getVelocity) that stores the speed in each direction in the array Vx, Vy by calculating the speed in the x direction and adding a code that returns Vx, Vy from the function written in #5.

This is the code I made

``````# Find the x-direction speed and the y-direction speed and write a function that stores it in the arrays Vx, Vy and return it.
def getVelocity(x,y,time):
Vx=(x-x)/(time-time)
Vy=(y-y)/(time-time)
return (Vx,Vy)
``````

I tried making it like this, but the output doesn't come out. I dare to ask you this question because I think it's right to write it like this.

python pycharm

2022-09-20 12:33

Although the intent of the problem is not known exactly, the code of the questioner would not be correct if the returned Vx, Vy should be an array.

I think he wanted the code as below.

``````def getVelocity(x, y, time):
Vx = []
Vy = []
x0 = x
y0 = y
t0 = t
for x1, y1, t1 in zip(x[1:], y[1:], t[1:]):
vx = (x1-x0)/(t1-t0)
vy = (y1-y0)/(t1-t0)
Vx.append(vx)
Vy.append(vy)
x0, y0, t0 = x1, y1, t1
return Vx, Vy

``````

2022-09-20 12:33

## If you have any answers or tips

Popular Tags
python x 4520
android x 1589
java x 1455
javascript x 1363
c x 881
php x 682
html x 629
swift x 586
ios x 538
mysql x 397