不懂Python方法

use*_*087 0 python artificial-intelligence

我不明白该move方法发生了什么.我正在从Udacity.com学习AI课程.视频位置为:http://www.udacity.com/view#Course/cs373/CourseRev/apr2012/Unit/512001/Nugget/480015

下面是我没有得到的代码,它没有按照视频中显示的那样工作.根据Udacity我应该得到的答案是[0,0,1,0,0]
这是我得到的 []

p=[0, 1, 0, 0, 0]


def move(p, U):
    q = []
    for i in range(len(p)):
        q.append(p[(i-U) % len(p)])
        return q

print move(p, 1)
Run Code Online (Sandbox Code Playgroud)

Roh*_*ain 6

压痕问题.你应该在for循环之外移动你的return语句,否则它将在第一次迭代后立即返回: -

for i in range(len(p)):
    q.append(p[(i-U) % len(p)])
return q
Run Code Online (Sandbox Code Playgroud)

而且,您的原始代码返回[0]而不仅仅是[].