以下是一个示例代码,演示了如何遍历形状并执行IncrementLeft操作。
class Shape:
def __init__(self, width, height):
self.width = width
self.height = height
def increment_left(self, increment):
self.width += increment
# 创建一个形状列表
shapes = [Shape(10, 20), Shape(15, 25), Shape(8, 12)]
# 遍历形状列表并执行IncrementLeft操作
for shape in shapes:
shape.increment_left(5)
# 打印每个形状的新宽度
for shape in shapes:
print(shape.width)
上述代码首先定义了一个Shape
类,该类具有width
和height
属性,并且还有一个increment_left
方法,用于将形状的左侧增加给定的增量。
然后,我们创建了一个形状列表shapes
,其中包含了三个不同的形状对象。
接下来,我们使用for
循环遍历形状列表,并对每个形状对象调用increment_left
方法,将左侧增加5个单位。
最后,我们再次使用for
循环打印每个形状对象的新宽度。
输出结果将是:
15
20
13
这表明每个形状对象的宽度都成功地增加了5个单位。
上一篇:遍历行以确定特定单词的计数