-
Notifications
You must be signed in to change notification settings - Fork 0
/
40. Turtle Triangles.py
60 lines (48 loc) · 1.46 KB
/
40. Turtle Triangles.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#Sanjidah Abdullah
#sabdull002@citymail.cuny.edu
#A template for a program that draws nested triangles
#Modified by: Sanjidah Abdullah
import turtle
def setUp(t, dist, col):
"""
Takes three parameters, a turtle, t, the distance, dist,
to move the turtle and a color, col, to set the turtle's color.
"""
t.penup()
t.forward(dist)
t.pendown()
t.color(col)
def triangle(t, length):
"""
Takes two parameters: a turtle and a length.
The function does the following: if the length is greater than 10,
it repeats 3 times: moves forward that length, turns 120 degrees,
and calls triangle(t, length/2).
"""
if length > 10:
for i in range (3):
t.forward(length)
t.left(120)
triangle(t, length/2)
def nestedTriangle(t, length):
"""
Takes two parameters: a turtle and a length.
The function does the following: if the length is greater than 10,
it repeats 3 times: moves forward that length, turns 120 degrees,
and calls triangle(t, length/2).
"""
if length > 10:
for i in range (3):
t.forward(length)
t.left(120)
nestedTriangle(t, length/2)
def main():
n = int(input('Enter length: '))
tom = turtle.Turtle()
setUp(tom, -100, "darkgreen")
triangle(tom, n)
tess = turtle.Turtle()
setUp(tess, 100, "steelblue")
nestedTriangle(tess, n)
if __name__ == "__main__":
main()