Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
| Download

📚 The CoCalc Library - books, templates and other resources

Views: 96169
License: OTHER
1
"""This module contains a code example related to
2
3
Think Python, 2nd Edition
4
by Allen Downey
5
http://thinkpython2.com
6
7
Copyright 2015 Allen Downey
8
9
License: http://creativecommons.org/licenses/by/4.0/
10
"""
11
12
from __future__ import print_function, division
13
14
import turtle
15
16
from polygon import arc
17
18
19
def petal(t, r, angle):
20
"""Draws a petal using two arcs.
21
22
t: Turtle
23
r: radius of the arcs
24
angle: angle (degrees) that subtends the arcs
25
"""
26
for i in range(2):
27
arc(t, r, angle)
28
t.lt(180-angle)
29
30
31
def flower(t, n, r, angle):
32
"""Draws a flower with n petals.
33
34
t: Turtle
35
n: number of petals
36
r: radius of the arcs
37
angle: angle (degrees) that subtends the arcs
38
"""
39
for i in range(n):
40
petal(t, r, angle)
41
t.lt(360.0/n)
42
43
44
def move(t, length):
45
"""Move Turtle (t) forward (length) units without leaving a trail.
46
Leaves the pen down.
47
"""
48
t.pu()
49
t.fd(length)
50
t.pd()
51
52
53
bob = turtle.Turtle()
54
55
# draw a sequence of three flowers, as shown in the book.
56
move(bob, -100)
57
flower(bob, 7, 60.0, 60.0)
58
59
move(bob, 100)
60
flower(bob, 10, 40.0, 80.0)
61
62
move(bob, 100)
63
flower(bob, 20, 140.0, 20.0)
64
65
bob.hideturtle()
66
turtle.mainloop()
67
68