IndexError: tuple index out of range in Turtle module

Viewed 15

IndexError: tuple index out of range in Turtle module here is my code

import turtle
t = turtle.Turtle()
s = turtle.Screen()
s.bgcolor("black"), t.speed(0)

col = ("yellow", "red", "pink", "cyan", "light", "green", "blue")

for i in range(150):
    t.pencolor(col[186])
    t.circle(198-1/2, 99)
    t.lt(90)
    t.circle(199-1/3, 98)
    t.lt(60)
s.exitonclick()
1 Answers

If you want to cycle through that list of colors, you've a few options. One option would be to use cycle from the itertools library. A simple option is to use the modulus operator (%) in combination with your iteration variable and the length of your colors list:

from turtle import Screen, Turtle

COLORS = ['yellow', 'red', 'pink', 'cyan', 'green', 'blue']

screen = Screen()
screen.bgcolor('black')

turtle = Turtle()
turtle.speed('fastest')

for i in range(150):
    turtle.pencolor(COLORS[i % len(COLORS)])
    turtle.circle(198, 99)
    turtle.lt(90)
    turtle.circle(199, 98)
    turtle.lt(60)

turtle.hideturtle()
screen.exitonclick()
Related