Toggle
Ben Finney
ben+python at benfinney.id.au
Wed Oct 8 21:42:14 EDT 2014
Seymore4Head <Seymore4Head at Hotmail.invalid> writes:
> I want to toggle between color="Red" and color="Blue"
It's good to cultivate ongoing familiarity with the standard library
<URL:https://docs.python.org/3/library/itertools.html#itertools.cycle>
so that you can make use of wheels already invented and maintained::
import itertools
colours = ["Red", "Blue"]
colour_cycle = itertools.cycle(colours)
next(colour_cycle) # → "Red"
next(colour_cycle) # → "Blue"
next(colour_cycle) # → "Red"
next(colour_cycle) # → "Blue"
next(colour_cycle) # → "Red"
for colour in colour_cycle:
# … loops indefinitely
# with ‘colour’ bound to each value in turn …
--
\ “My roommate got a pet elephant. Then it got lost. It's in the |
`\ apartment somewhere.” —Steven Wright |
_o__) |
Ben Finney
More information about the Python-list
mailing list