Think Python 2e: Flower Drawing Code Explained
Hey guys! Today, we're diving deep into a cool Python code snippet from "Think Python 2e," specifically from category 4.2. This code uses the turtle
module to draw some pretty interesting flowers. We'll break down the code step by step, so you can understand how it works and even tweak it to create your own floral designs. Let's get started!
Understanding the Basics: The turtle
Module
First off, let's talk about the turtle
module. Think of it as a digital Etch-A-Sketch. You have a virtual turtle that you can command to move around the screen, and as it moves, it draws lines. It's a super fun and visual way to learn programming concepts, especially loops and functions. This module is what makes it possible for our Python code to draw the flower pattern that we see here.
To use the turtle
module, you first need to import it:
import turtle
import math
bob = turtle.Turtle()
Here, we're importing the turtle
module and creating a Turtle
object named bob
. You can name your turtle anything you like, but bob
is a classic choice! We also import the math
module, which we'll need later for some calculations.
Diving into the flower1
Function
Now, let's break down the first function, flower1(t, a)
. This function is the heart of our flower-drawing program. It takes two arguments: t
, which is our turtle object, and a
, which determines the shape and size of the flower petals.
def flower1(t, a):
r = 10 * a
n = int(4 * r * 2 * math.pi) + 1
f = int(n / int(a)) + 1
for i in range(f):
t.lt(360 / n)
t.fd(0.25)
t.lt(180 * (n - 2) / n - 360 / a)
for i in range(f):
t.lt(360 / n)
t.fd(0.25)
t.lt(180)
Let's dissect this piece by piece:
r = 10 * a
: This line calculates the radius of the arcs that form the petals. The radius is directly proportional toa
, so a largera
means larger petals.n = int(4 * r * 2 * math.pi) + 1
: This line calculates the number of steps needed to draw a smooth arc. It's based on the radiusr
and involvesmath.pi
to get the circumference of a circle. Theint()
function ensures we have a whole number.f = int(n / int(a)) + 1
: This calculates how many times the loop should run to draw each petal segment. It depends onn
(the number of steps) anda
(the petal shape factor).for i in range(f): ...
: This is the main loop that draws one segment of a petal. Inside the loop:t.lt(360 / n)
: Turns the turtle left by a small angle. This angle is calculated to create a smooth curve.t.fd(0.25)
: Moves the turtle forward a tiny bit, drawing a small line segment.
t.lt(180 * (n - 2) / n - 360 / a)
: This line adjusts the turtle's heading after drawing one segment, preparing it to draw the next part of the petal. It's a bit of trigonometric magic!- The second
for
loop is nearly identical to the first, drawing the other half of the petal segment. t.lt(180)
: This turns the turtle around 180 degrees, likely positioning it for the next petal or flower element.
In essence, flower1
draws one curved segment, adjusts the turtle's position, draws another curved segment (completing a petal-like shape), and then repositions the turtle. The number of these shapes and their form is dictated by the value of a
.
Exploring the flower2
Function
Next up, we have the flower2(t, b)
function. This one builds upon flower1
to create a different style of flower. It takes the turtle object t
and another parameter b
.
def flower2(t, b):
flower1(t, int(b / 2))
t.rt(180 - 720 / b)
Here's what's happening:
flower1(t, int(b / 2))
: This line calls theflower1
function we just dissected, but it passes inint(b / 2)
as thea
parameter. This meansflower2
reuses the logic offlower1
to draw a petal shape, but the shape is influenced byb / 2
instead ofa
directly.t.rt(180 - 720 / b)
: This is where things get interesting. After drawing the petal-like shape usingflower1
, this line turns the turtle right by a specific angle. This angle is calculated based onb
and determines the spacing between the petals in the final flower.
So, flower2
draws one petal shape (using flower1
) and then rotates the turtle. This makes it easy to draw flowers with multiple petals arranged in a circle. The key difference from flower1
is this rotation step, which allows for creating multi-petaled flowers.
Putting It All Together: The Main Program
Now, let's look at the main part of the program, where the user interacts and the flowers are actually drawn.
t = int(input("input type of flower:")) # 1 or 2
if t == 1:
a = int(input("input the number of pental type 1:")) # for example picture 1 and 3
for i in range(a):
flower1(bob, a)
elif t == 2:
b = int(input("input the number of pental type 2:")) # for example picture 2
for i in range(b):
flower2(bob, b)
else:
print("eror")
turtle.mainloop()
Here’s a breakdown:
- **`t = int(input(