[英]IndexError: tuple index out of range in Turtle module
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()
如果您想循環瀏覽 colors 的列表,您有幾個選擇。 一種選擇是使用itertools
庫中的cycle
。 一個簡單的選擇是將模運算符 (%) 與迭代變量和 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.