0

I have this Python program that prints out colors from a predefined list called "UserColorIndex" - I want the program printing those colors depending of the number value for the variable called "NumberOfCircles." So if there were 100 in the value for the NumberOfCircles, then the program should print out those colors from the list 100 times, and if the index is only 9 colors, then the program should loop through those colors and repeat through those colors to get them printed. I tried using the enumerate method but that just created a different data type. How would I fix/do this?

Here is my code:

NumberOfCircles = 18    # I don't know, just some random number, the program should work regardless of which number is placed 

def GenerateRosette():
    for i in range(NumberOfCircles):
        print(UserColorIndex[i])

UserColorIndex = ["Red", "Orange", "Yellow", "Green", "Blue", "Indigo", "Violet", "Black", "Grey"]

GenerateRosette()
Output:
________________

Red
Orange
Yellow
Green
Blue
Indigo
Violet
Black
Grey
Traceback (most recent call last):
  File "file.py", line 9, in <module>
    GenerateRosette()
  File "file.py", line 5, in GenerateRosette
    print(UserColorIndex[i])
IndexError: list index out of range
EXPECTED Output (What I want):
________________

Red
Orange
Yellow
Green
Blue
Indigo
Violet
Black
Grey
Red
Orange
Yellow
Green
Blue
Indigo
Violet
Black
Grey

In the expected output, I want the list to be printed depending on how many times the for loop was run (NumberOfCircles). I want it to loop through the list. How would I do this?

8
  • You can try using mod (%). UserColorIndex[ i % len(UserColorIndex)]. This would start again from 0 when loop ends Commented Apr 20, 2021 at 0:26
  • A while seems easier. Commented Apr 20, 2021 at 0:30
  • How would you use a while loop for this? Please explain as I am new to Python and am just learning :) Commented Apr 20, 2021 at 0:32
  • 1
    Oh, no I wanted to repeatedly loop the list depending on the NumberOfCirlces Value. Commented Apr 20, 2021 at 0:39
  • 1
    Have you checked your output? It seems your expecting it to loop 18 times. Commented Apr 20, 2021 at 0:57

2 Answers 2

2

The easy way is to use itertools.cycle:

import itertools

UserColorIndex = ["Red", "Orange", "Yellow", "Green", "Blue", "Indigo", "Violet", "Black", 
"Grey"]

def GenerateRosette(n):
    color = itertools.cycle(UserColorIndex)
    for _ in range(n):
        print(next(color))

NumberOfCircles = 16
GenerateRosette(NumberOfCircles)
Sign up to request clarification or add additional context in comments.

1 Comment

You should use islice here, not manually call next
1

I found the original solution from @Tim to be elegant, and the enhancement suggested by @Juanpa to be insightful. Combining the two produces the following snippet that seems extremely "Pythonic" to me:

import itertools

UserColorIndex = ["Red", "Orange", "Yellow", "Green", "Blue", "Indigo", "Violet", "Black", 
"Grey"]

def GenerateRosette(n):
    for color in itertools.islice(itertools.cycle(UserColorIndex), n):
        print(color)

NumberOfCircles = 16
GenerateRosette(NumberOfCircles)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.