我有一些我认为是正确的代码。但是,它应该返回值的向量,但它返回的是单个累积值。
pExact = 0.8
pOvershoot = 0.1
pUndershoot = 0.1
def move(p, U):
q = []
for i in range(len(p)):
s = pExact * p[(i - U) % len(p)]
s = s + pOvershoot * p[(i - U - 1) % len(p)]
s = s + pUndershoot * p[(i - U + 1) % len(p)]
print i, s # check if calculations are corrects
q.append(s)
return q # should return a vector
p = [0, 1, 0, 0]
print move(p, 1) # prints only cumulated value
我试图理解为什么它只打印一个值 [0.10000000000000001] 而不是矢量 [0, 0.1, 0.8, 0.1] 我认为它应该如何。
请您参考如下方法:
q.append(s) 中有错误的缩进。它应该在 for 循环中。
这是正确的版本:
pExact = 0.8
pOvershoot = 0.1
pUndershoot = 0.1
def move(p, U):
q = []
for i in range(len(p)):
s = pExact * p[(i - U) % len(p)]
s = s + pOvershoot * p[(i - U - 1) % len(p)]
s = s + pUndershoot * p[(i - U + 1) % len(p)]
print i, s # check if calculations are corrects
q.append(s)
return q # should return a vector
p = [0, 1, 0, 0]
print move(p, 1) # prints only cumulated value
