1
import turtle
tina = turtle.Turtle()
tina.shape('turtle')
your_name = input("What is your name")

tina.penup()
tina.forward(20)
tina.write("Why, hello there, " + your_name + "!")
tina.backward(20)
tina.color("green")
tina.left(90)
tina.forward(100)
tina.right(90)
tina.pendown()
tina.pencolor("red")
tina.forward(50)
tina.right(50)
tina.forward(50)
tina.right(100)
tina.forward(55)
tina.left(50)
tina.forward(55)
tina.penup()
tina.forward(30)
tina.pendown()
tina.dot(10)
tina.penup()
tina.goto(100, 100)
color = input("What color is the shape")
try:
  if color == ("red"):
    tina.write("Your are correct" + your_name + "!")
    tina.backward(20)
  elif color == ("green" or "Green"):
    tina.write("Sorry, It is actually Red")
    tina.backward(20)
  elif color == ("black" or "Black"):
    tina.write("Sorry, Its is actually Red")
    tina.backward(20)
  elif color == ("purple" or "Purple"):
    tina.write("Sorry, It is actually Red")
    tina.backward(20)
  elif color == ("blue" or "Blue"):
    tina.write("Sorry, It is actually Red")
    tina.backward(20)
except:
  tina.backward(20)
  tina.write("Sorry, but that isn't a color")
  tina.backward(20)

This is my code. I would like to know how to make the turtle change colors randomly through out the whole program. It should be changing color every .5 seconds through out the whole program. How should I do this. I tried to import random. This is for a school assignment. Please Help.

  • 2
    Incidentally, `elif color == ("green" or "Green"):` doesn't do what you want. See [How do I test one variable against multiple values?](http://stackoverflow.com/q/15112125/953482) for more information. – Kevin Oct 01 '15 at 13:55
  • What did you try? And do you want the color to change in random time intervalls, or every .5 seconds? Did you have a look at `while` loops? – chris-sc Oct 01 '15 at 14:02
  • i want it to change every .5 seconds –  Oct 01 '15 at 14:50
  • Have a look at the `turtle.ontimer()` function and `random.choice()`. – BlackJack Oct 01 '15 at 16:16

1 Answers1

0

First thing, you should break your code into functions, for example:

def drawShape (myTurtle, turtleColor, shapeColor):
    myTurtle.penup()
    myTurtle.backward(20)
    myTurtle.color(turtleColor)
    myTurtle.left(90)
    myTurtle.forward(100)
    myTurtle.right(90)
    myTurtle.pendown()
    myTurtle.pencolor(shapeColor)
    myTurtle.forward(50)
    myTurtle.right(50)
    myTurtle.forward(50)
    myTurtle.right(100)
    myTurtle.forward(55)
    myTurtle.left(50)
    myTurtle.forward(55)
    myTurtle.penup()
    myTurtle.forward(30)
    myTurtle.pendown()
    myTurtle.dot(10)
    myTurtle.penup()
    myTurtle.goto(100, 100)

You can create random numbers using random:

import random
randomNumber = random.randint(0,5) #created random integer between 0 and 5

Now in order to use random for colors, first create a list of colors:

myColorList = ["red","green","black","purple","blue","yellow"]

and then create a random integer, and use that integer as the index to this list.

When checking the answer, you don't need that many ifs. Instead, check if color in myColorList - and if it's not, you report Sorry, but that isn't a color

To change the turtle color (or in general, to do something) based on a timer, use turtle.ontimer() function. When you call ontimer, you give it a function and a number representing miliseconds. A very good example is here. In this example you can see they call ontimer at the end of the function passed to ontimer, so that it keeps happening on and on, instead of just once.