6

At the moment I have defined many shapes in Turtle using begin_poly and end_poly then register_shape. I want to be able to put all of these values into a list and, with a press of a button, cycle through the list hence changing the Turtle shape. I am having difficulty achieving this with Itertools and was wondering for some assistance on how I could achieve this.

Edit: I got it working in the end, I appended all the values into a list then used a counter to choose which index to go to.

Hayden
  • 229
  • 2
  • 5
  • 12
  • Explain how it "doesn't work". http://sscce.org/ – Marcin Aug 31 '12 at 02:08
  • It looks like the problem is that you don't actually do anything with the values obtained from the generator. – Marcin Aug 31 '12 at 02:09
  • I edited the question. When I tried this, it goes to a shape but when I press h again, it won't move off of it. It ghosts to other shapes so I know that it is cycling but it isn't setting to a new shape. – Hayden Aug 31 '12 at 02:34
  • Can you simplify your entire code to something you can post? I think you have more problems than using a generator. – Mark Tolonen Aug 31 '12 at 02:36
  • Here is the code for the shapes. I did from turtle import *. – Hayden Aug 31 '12 at 02:40
  • This is, if anything, even less comprehensible now. Your code isn't even indented correctly. I am voting to close as not a real question. – Marcin Aug 31 '12 at 02:48

1 Answers1

34

First, create the generator:

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

Then call next() whenever you want another one.

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

Here's a simple program:

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

Output:

Drawing square
Drawing triangle
Drawing circle
Drawing pentagon
Drawing star
Drawing octagon
Drawing square
Drawing triangle
Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
  • I tried what I did in the edited question but it still isn't working. :S – Hayden Aug 31 '12 at 02:04
  • -1 how does this take OP beyond what they have in the question? – Marcin Aug 31 '12 at 02:10
  • 4
    @Marcin: Most of the code in the question is *from* this answer. Originally it seemed like the OP was constructing a new `cycle` instance every time, although it's hard to tell from `next (itertools.cycle (shape_list)) 1 next (itertools.cycle (shape_list)) 2`, whatever that might mean. This answer separated out the generator creation from the getting of the next element, which was a perfectly plausible reading of the OP's problem. – DSM Aug 31 '12 at 02:16
  • Unfortunately Mark the code still doesn't work. :( Am I doing something wrong? – Hayden Aug 31 '12 at 02:21
  • @Marcin, the OP has edited his question. As DSM says the original question wasn't clear. – Mark Tolonen Aug 31 '12 at 02:23
  • `g.next()` doesn't work in Python 3: _AttributeError: 'itertools.cycle' object has no attribute 'next'_ You need to call `next(g)` – richar8086 Feb 05 '17 at 18:49