不懂Python方法[关闭](Don't understand Python Method [closed])

编程入门 行业动态 更新时间:2024-10-06 16:25:53
不懂Python方法[关闭](Don't understand Python Method [closed])

我不明白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)

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)

最满意答案

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

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

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

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 [].

更多推荐

本文发布于:2023-08-07 12:58:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464066.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不懂   方法   Python   closed   understand

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!