user836087 user836087 - 5 months ago 7
Python Question

Don't understand Python Method

I don't understand what is going on in the

move
method. I am taking the AI course from Udacity.com. The video location is: http://www.udacity.com/view#Course/cs373/CourseRev/apr2012/Unit/512001/Nugget/480015

Below is the code I don't get, it's not working as shown in the video ..
The answer I should be getting according to Udacity is [0, 0, 1, 0, 0]

Here is what I get
[]


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)

Answer

Indentation problem. You should move your return statement outside the for loop, else it will return immediately after the first iteration: -

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

And also, your original code returns [0] and not just [].

Comments