Practice defining your own functions to use in a Python Turtle drawing First, de
ID: 3774553 • Letter: P
Question
Practice defining your own functions to use in a Python Turtle drawing First, define a function called getColor. Just like the built in bin() function returns a number, your getColor() is going to return a string with a color value. You will pick one of three colours to randomly return. To do this. you first have to import random (you can write this immediately after import turtle), as we have done before in class. Than you can store the result of calling random, rending 1.3) in a variable. The function randint() returns a random whole number that is at least 1 and at most 3 (1Explanation / Answer
import turtle
import random
def getColor():
selection = random.randint(1,3)
if selection == 1:
return "green"
elif selection == 2:
return "red"
else:
return "blue"
def drawPerson(my_turtle, x, y):
my_turtle.penup()
my_turtle.goto(x,y)
my_turtle.pendown()
my_turtle.pensize(3)
xul.color = getColor()
print(xul.color)
if xul.color == "green":
xul.color("green")
elif xul.color == "red":
xul.color("red")
else:
xul.color("blue")
xul.forward(90)
xul.left(90)
xul.forward(90)
xul.left(90)
xul.forward(90)
xul.left(90)
xul.forward(90)
xul.left(90)
xul = turtle.Turtle()
drawPerson(xul,150,150)
drawPerson(xul,-150,150)
drawPerson(xul,-150,-150)
drawPerson(xul,150,-150)
drawPerson(xul,0,0)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.