Lab 2

Lab description:

In this lab we used python to make the turtle move in a random direction 100 times. The first step of the lab was to import the turtle. After importing the turtle you also had to import the random function. After naming the turtle, you then had to give certain things specifications such as the screen color, turtle color, and the shape of the turtle. As you can see in the script down below we made the color of the screen red. We also made the turtle white and made the shape turtle. After all these specifications were set, you then had to set its movement. The first part of this was to set the range to 100 so it repeated the process 100 times.  You then set it to pick a random direction by giving it to numbers to pick between. In this case the numbers were (0,45) and (0,150).

Script :

import turtle
import random
wn = turtle.Screen()
wn.bgcolor("red")
alex = turtle.Turtle()
alex.pencolor("white")
alex.shape ("turtle")
for i in range(100) :
 angle = random.randint(0,45)
 print (angle)
 alex.right(angle)
 distance1 = random.randint(0,150)
 print (distance1)
 alex.forward(distance1)
 alex.backward(distance1)

Screenshot:

turtle script

turtle script output

Leave a Reply

Your email address will not be published. Required fields are marked *