5

目前我已经定义了许多使用然后Turtlebegin_poly形状。我希望能够将所有这些值放入一个列表中,然后按一下按钮,循环浏览列表,从而改变形状。我很难实现这一点,并且想知道如何实现这一点的一些帮助。end_polyregister_shapeTurtleItertools

编辑:最后我得到了它,我将所有值附加到一个列表中,然后使用一个计数器来选择要转到哪个索引。

4

1 回答 1

34

首先,创建生成器:

>>> import itertools
>>> shape_list = ["square", "triangle", "circle", "pentagon", "star", "octagon"]
>>> g = itertools.cycle(shape_list)

然后next()在你想要另一个时打电话。

>>> next(g)
'square'
>>> next(g)
'triangle'
>>> next(g)
'circle'
>>> next(g)
'pentagon'
>>> next(g)
'star'
>>> next(g)
'octagon'
>>> next(g)
'square'
>>> next(g)
'triangle'

这是一个简单的程序:

import itertools
shape_list = ["square", "triangle", "circle", "pentagon", "star", "octagon"]
g = itertools.cycle(shape_list)
for i in xrange(8):
    shape = next(g)
    print "Drawing",shape

输出:

Drawing square
Drawing triangle
Drawing circle
Drawing pentagon
Drawing star
Drawing octagon
Drawing square
Drawing triangle
于 2012-08-31T01:46:18.763 回答