انتقل للمحتوى
Robo EgyptRobo Egypt
☰ Course contents
Robo Python: home
0. Lab tour1
1. The start5
2. Variables and input5
3. Conditions5
4. Loops5
5. Lists4
6. Functions4
7. Drawing with the turtle3
8. The project: the guess-the-number game3
9. Strings in depth1
10. Dictionaries (dict)3
11. Randomness and errors3
12. Classes and objects (class)2
13. Pro drawing and charts3
14. The final project: the quiz game2

A rose with functions

Section 13: Pro drawing and chartsLesson 13.1 · ⏱ 10 min
🎯 Project goal
A rose of 12 coloured petals. We draw one petal in a function, then repeat it as we turn.
The rose we will draw, as it appeared in Robo Python
The rose we will draw, as it appeared in Robo Python

t.circle(60, 60) draws an arc (part of a circle with radius 60, at an angle of 60). Two arcs facing each other make a petal. colors[i % 6] starts the colours again after the sixth colour.

import turtle

t = turtle.Turtle()
t.speed(0)
colors = ["red", "orange", "gold", "green", "blue", "purple"]

def petal():
    t.circle(60, 60)
    t.left(120)
    t.circle(60, 60)
    t.left(120)

for i in range(12):
    t.color(colors[i % 6])
    petal()
    t.right(30)
t.hideturtle()
🐍 Open the code in Robo Python ↗
  1. Make it 18 petals and a 20-degree turn.
  2. Change the radius to 100 and notice the rose's shape.

📌 Another example: a flower with six petals

A petal function draws a petal, and we repeat it 6 times.

🐍 A flower with six petals
import turtle
t = turtle.Turtle()
t.speed(0)
t.color("deeppink")

def petal():
    t.circle(60, 60)
    t.left(120)
    t.circle(60, 60)
    t.left(120)

for i in range(6):
    petal()
    t.left(60)
🐍 Open the code in Robo Python ↗
In Robo Python
In Robo Python
✍️ Practise
Try the lesson's examples here: copy the code, change it, and click ▶ Run.
✍️ Practise here in the lessonor open the lab in a new page ↗

🎯 Challenge yourself

Easy

A flower with eight petals (a 45 turn).

👀 Show the solution
🐍 Solution
import turtle
t = turtle.Turtle()
t.speed(0)
t.color("orange")

def petal():
    t.circle(60, 60)
    t.left(120)
    t.circle(60, 60)
    t.left(120)

for i in range(8):
    petal()
    t.left(45)
🐍 Open the code in Robo Python ↗
In Robo Python
In Robo Python

Result: eight petals.

Medium

A flower with a green stem under it.

👀 Show the solution
🐍 Solution
import turtle
t = turtle.Turtle()
t.speed(0)
t.color("red")

def petal():
    t.circle(50, 60)
    t.left(120)
    t.circle(50, 60)
    t.left(120)

for i in range(6):
    petal()
    t.left(60)
t.color("green")
t.pensize(4)
t.right(90)
t.forward(150)
🐍 Open the code in Robo Python ↗
In Robo Python
In Robo Python

Result: a flower with a stem.

Hard

Three flowers in different colours (a flower(x, color) function).

👀 Show the solution
🐍 Solution
import turtle
t = turtle.Turtle()
t.speed(0)

def petal(size):
    t.circle(size, 60)
    t.left(120)
    t.circle(size, 60)
    t.left(120)

def flower(x, color):
    t.penup()
    t.goto(x, 0)
    t.pendown()
    t.color(color)
    for i in range(6):
        petal(40)
        t.left(60)

flower(-140, "red")
flower(0, "gold")
flower(140, "purple")
🐍 Open the code in Robo Python ↗
In Robo Python
In Robo Python

Result: three flowers.

Finished the lesson? Mark it so your progress counts.