Python Ship Movement Speed Function Question

Asked 2 years ago, Updated 2 years ago, 71 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[31]-x[0])/(time[31]-time[0])
    Vy=(y[31]-y[0])/(time[31]-time[0])
    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

1 Answers

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[0]
    y0 = y[0]
    t0 = t[0]
    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


© 2024 OneMinuteCode. All rights reserved.