0

I have two arrays:

  1. colors - Contains color names, represented by a list type;
  2. string - String with some phrase, represented by a string type.

Code:

colors = ["red", "black", "blue",]
string = "Hello World"


def animate():
    for i in range(len(string)):
        print "{0} - {1}".format(string[i], colors[i%len(colors)])
    print "Next iteration..."

#while True:
for i in range(3): # Short example loop - range(3)
    animate()

Output:

H - red
e - black
l - blue
l - red
o - black
Next iteration...
H - red
e - black
...
And so on...

The goal: each new next iteration has to begin with next color i.e.

H - red  # Begins with first color
e - black
l - blue
l - red
o - black
Next iteration...
H - black # Begins with second color
e - blue
...
Next iteration...
H - blue # Begins with third color
e - red
...      # Next one begins again with the first.
And so on...

What ways are there to modify my code? Can it be implemented inside the animate function only? I was thinking about python generator functions (yield), but finale loop where animate() is resets generator function.

3 Answers 3

1

Pass in a counter and start your colors from this counter:

colors = ["red", "black", "blue",]
text = "Hello World"


def animate(counter):
    for index, char in enumerate(text):
        color_index = (index + counter) % len(colors)
        print "{0} - {1}".format(char, colors[color_index])
    print "Next iteration..."

#while True:
for i in range(3): # Short example loop - range(3)
    animate(i)

Output:

H - red
e - black
l - blue
l - red
o - black
  - blue
W - red
o - black
r - blue
l - red
d - black
Next iteration...
H - black
e - blue
l - red
l - black
o - blue
  - red
W - black
o - blue
r - red
l - black
d - blue
Next iteration...
H - blue
e - red
l - black
l - blue
o - red
  - black
W - blue
o - red
r - black
l - blue
d - red
Next iteration...
Sign up to request clarification or add additional context in comments.

2 Comments

Made it a bit more pythonic by using enumerate.
Yes, I see. Perfect for me.
0

There are multiple ways to achieve your goal here, and it can certainly be implemented just inside of the animate function. I assume you want to run it exactly as many times as there are colors? One of the easier ways I can think of to do this, with minimal code changes, is through recursion.

The basic rundown would be:

  • Define a location (inside the color array) of the color you'd like to start with, with the default being location 0.
  • Print the string and color, starting with the color you want
  • If you haven't reached the last color at the end of the function, increment the color location and have your animate function call itself.

This may not be completely clear, so I'll show you what I mean in code:

colors = ["red", "black", "blue",]
string = "Hello World"

def animate(location = 0):
    for i in range(len(string)):
        print "{0} - {1}".format(string[i], colors[(i + location)%len(colors)])
    if (location < len(colors) - 1):
        print "Next iteration..."
        animate(location + 1)

animate()

You can see it work here. Notice that I pull the displayed color by using colors[(i + location) % len(colors)]

If something isn't clear, please let me know.

Edit: The recursion is only necessary if you want to contain the code only inside of the animate function. If you don't mind running a loop, you can just pass in a counter as in other answers.

1 Comment

"I assume you want to run it exactly as many times as there are colors" No, it would be using 'while' loop. Thanks, I've already got right answer.
0

Python's itertools.cycle would be useful here:

from itertools import cycle

colors = ["red", "black", "blue",]
text = "Hello World"
color = cycle(colors)

def animate():
    for c in text:
        print '{} - {}'.format(c, next(color))
    print "Next iteration..."

for i in range(3): # Short example loop - range(3)
    animate()

This would display the following:

H - red
e - black
l - blue
l - red
o - black
  - blue
W - red
o - black
r - blue
l - red
d - black
Next iteration...
H - blue
e - red
l - black
l - blue
o - red
  - black
W - blue
o - red
r - black
l - blue
d - red
Next iteration...
H - black
e - blue
l - red
l - black
o - blue
  - red
W - black
o - blue
r - red
l - black
d - blue
Next iteration...

3 Comments

Good. It seems pytonic.
But the output is not right. The second iteration should start with black but its starts with blue.
pythonic* But the second iteration starts with blue instead of black.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.