Matthew H-M
Matthew H-M

Reputation: 1

Changing color while in a repeat in turtle

I am a python beginner and am working on a basic assignment to write my initials with turtle. I have everything set up with the process of my initials being repeated working properly, however, I need the color to change after each repeat. After extensive research, I could not get it to work. Here is my code.

import turtle

screen = turtle.Screen()
screen.setup(400,400,0,0)

screen.bgcolor('black')

timmy = turtle.Turtle()
timmy.color('orange')

timmy.shape("turtle")

timmy.pencolor("purple")
timmy.pensize(7)

for i in range(0,4):

  timmy.penup()
  timmy.goto(-150,-150)

  timmy.pendown()

  timmy.left(90)
  timmy.forward(150)
  timmy.right(165)
  timmy.forward(155)
  timmy.left(150)
  timmy.forward(155)
  timmy.right(165)
  timmy.forward(150)

  timmy.penup()

  timmy.left(90)
  timmy.forward(30)
  timmy.left(90)

  timmy.pendown()

  timmy.forward(150)
  timmy.backward(75)
  timmy.right(90)
  timmy.forward(50)
  timmy.left(90)
  timmy.forward(75)
  timmy.left(180)
  timmy.forward(150)

  timmy.penup()

  timmy.left(90)
  timmy.forward(30)

  timmy.pendown()
  timmy.left(90)
  timmy.forward(150)
  timmy.right(165)
  timmy.forward(155)
  timmy.left(150)
  timmy.forward(155)
  timmy.right(165)
  timmy.forward(150)
  timmy.left(90)

Upvotes: 0

Views: 4826

Answers (2)

cdlane
cdlane

Reputation: 41872

Since you don't reference the i variable that controls the number of iterations:

timmy.pencolor("purple")

for i in range(0,4):

    ...

We can instead do:

PEN_COLORS = ["purple", "red", "green", "orange"]

for color in PEN_COLORS:

  timmy.pencolor(color)

  ...

Letting the number of colors control the number of iterations.

Upvotes: 1

Jonath P
Jonath P

Reputation: 571

Right after you start your loop for i in range(0,4), you can add :

if i == 0 : timmy.pencolor('NAMEOFCOLOR')
if i == 1 : timmy.pencolor('NAMEOFCOLOR')
if i == 2 : timmy.pencolor('NAMEOFCOLOR')
if i == 3 : timmy.pencolor('NAMEOFCOLOR')

Upvotes: 0

Related Questions