Professional Documents
Culture Documents
import turtle
# This creates our window.
window = turtle.Screen()
# This creates our actual 'turtle' that will be used to move around the screen and draw
# different things. You can name it anything you like but I called it pencil to help
# remember that it is responsible for the drawing.
7. pencil = turtle.Turtle()
8. pencil.forward(100)
9.
10. window.exitonclick()
When you run the program, what happens? IT DRAWS A LINE! Impressive, right? Well lets expand
our drawing skills. The commands below show you some other powers of turtle. Most of the method
names give hints as to what they actually do. Experiment with these methods and the template below.
Work on becoming more confident with each of the methods, youll use them more and more.
1. pencil.forward(25)
2. pencil.backward(50)
3. pencil.left(90)
4. pencil.right(720)
5.
6. pencil.penup()
7. pencil.pendown()
8.
9. pencil.goto(50,-200)
10. pencil.goto(-150,300)
11.
12. pencil.hideturtle()
13. pencil.showturtle()
14.
15. pencil.pensize(10)
16. pencil.speed(5)
17. pencil.color('red')
18.
19. pencil.write("Hello student!", move=False, align="left", font=("Arial", 14, "bold"))
20.
21. #the best one!!!!
22. pencil.shape("turtle")
Feeling pretty confident with each of these commands? Ok! Try these challenges.
Challenge #1A - Draw a blue triangle.
Challenge #1B - Draw a right angle green triangle with legs of length 100 and 60.
Challenge #1C - Draw a red isosceles triangle and label it as an Isosceles Triangle with black text.
print(6+2)
print(6-2)
print(6*2)
print(6/2)
print(6**2)
print(abs(-12))
print(7 % 2)
Can you figure out what each example is doing? Many of them are pretty easy but 7 % 2 might be a little
tricky. Another way of reading this is 7 modulo 2 and it represents the remainder after division. For 7 %
2 the remainder after division is 1 because 2 goes into 7 a total of 3 times with a remainder of 1.
Great! But you might be thinking that you still are not able to apply the Pythagorean Theorem because
you dot not have the ability to apply the square root. Well just like we had to import the turtle library
so that we are able to use all of its methods, we also have to import a library called math (very
appropriate name!) to get access to additional operations and methods.
1. import math
2.
3. #convert from radians to degrees and degrees to radians
4. print(math.degrees(3.14))
5. print(math.radians(360))
6.
7. #trig ratios (must be given in radians)
8. print(math.sin(math.radians(30)))
9. print(math.cos(math.radians(60)))
10. print(math.tan(math.radians(45)))
11.
12. #inverse trig ratios (returns radians)
13. print(math.asin(0.5))
14. print(math.acos(math.sqrt(2)/2))
15. print(math.atan(math.sqrt(3)))
16.
17. #whole bunch of other functions
18. print(math.sqrt(16))
19. print(math.factorial(5))
20. print(math.pow(2, 5))
The documentation for the math module includes all the other functions that are available
(https://docs.python.org/3/library/math.html).
Challenge #2A - See if you can draw a random right-angle triangle with legs (the two sides that are not
the hypotenuse) between 100 and 300 units in length. Label the three sides of the triangle as well as the
angles.
1. import turtle
2. import random
3.
4. window = turtle.Screen()
5.
6. pointer = turtle.Turtle()
7.
8. pointer.forward(random.randint(100,300))
9.
10. window.exitonclick()
decimal_value = 132.43414
#you can round off decimal values using this method
print(round(decimal_value, 2))
print(round(decimal_value, 4))
Challenge #2B We are writing many different things to the screen (side lengths and angles) and we
are doing it over and over again. You probably notice that it repeats a lot of the same code. Try to create
a function that lets you write any message you want onto different locations on the screen.
1.
2.
3.
4.
5.
6.
7.
8.
Challenge #2C Can you find any other ways to make custom functions that might be useful? What if
we wanted to draw new triangles? Can you make a function called draw_triangle that does exactly what
its name says it draws a triangle with specific side lengths and angles.
question_type = random.randint(0,3)
if question_type == 0:
#make this type of question
elif question_type == 1:
#make this type of question
# and so on and so on
Challenge #3B Have any other ideas for your problem generator? Feel free to challenge yourself and
see what you can come up with.