【发布时间】:2019-03-19 01:07:57
【问题描述】:
这是我需要转换为 while 循环的 for 循环。我认为这会起作用,但它给了我一个没有移动属性的错误。这是一个创建人脸图形图像的程序,因此“shapeList”中的所有“形状”都是头部、鼻子、嘴巴、眼睛。面需要沿着窗口的边缘移动。
def moveAll(shapeList, dx, dy):
for shape in shapeList:
shape.move(dx, dy)
def moveAll(shapeList, dx, dy):
shape = []
while shape != shapeList:
shapeList.append(shape)
shape.move(dx, dy)
【问题讨论】:
-
输入和预期输出是什么?
-
for循环看起来正确。为什么要改变它? -
for 循环是正确的。任务是将其切换到 while 循环,但我遇到了属性错误。
-
好吧,
shapeList.append(shape)只是将shape(始终为[])附加到shapeList,而您在一个空列表(即[])上调用move(dx,dy).这就是你得到这个错误的原因。 -
你应该做的是反复从
shapeList中取出一个元素,并调用move(dx,dy),直到你访问了shapeList中的每个元素。
标签: python loops for-loop while-loop