简体   繁体   English

IndexError:Turtle 模块中的元组索引超出范围

[英]IndexError: tuple index out of range in Turtle module

IndexError: tuple index out of range in Turtle module here is my code IndexError:Turtle 模块中的元组索引超出范围这是我的代码

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()

If you want to cycle through that list of colors, you've a few options.如果您想循环浏览 colors 的列表,您有几个选择。 One option would be to use cycle from the itertools library.一种选择是使用itertools库中的cycle A simple option is to use the modulus operator (%) in combination with your iteration variable and the length of your colors list:一个简单的选择是将模运算符 (%) 与迭代变量和 colors 列表的长度结合使用:

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM