Custom Search

PYTHON: Turtle Graphics

- Drawing a flower

- the user choosing the number of petals

#Import the libraries to be used in the program

import turtle

import math

# set up the screen

screen=turtle.Screen()

screen.setup(550,450)

screen.bgpic("background.png")#set an image as the background

screen.addshape("FuzzlePINK.png")

#Set the turtle up

fuzzle = turtle.Turtle("FuzzlePINK.png")

fuzzle.shape("FuzzlePINK.png")#Set the turtle to be a Fuzzle

fuzzle.color("pink")

fuzzle.fillcolor("purple")

fuzzle.width(2)#set width of drawn line

fuzzle.up()

fuzzle.goto(0,-35)#set position of the turtle at centre of screen

fuzzle.left(90)#rotate to set turtle pointing up

#Set variables to zero

number=0

count=0

#ask for user input

while True:

try:

number=int(input("Please choose the number of petals you want the flower to have:"))

break

except ValueError:

print("")

print("")

print("You can only enter whole numbers - not decimals, letters or symbols.")

print("")

#Guiding the user to choose a number in a range that will be sensible

while number<3 or number>125:

if number<3:

print("")

print("")

print("To draw a flower you must have a minimum of 3 petals...")

if number>125:

print("The number of points you have chosen is so great that the petals would be too close together and the figure would look like a circle.")

print("")

print("")

number=int(input("Please input the number of petals you want on the flower:"))

#draw the flower

fuzzle.down()

while count<number:

fuzzle.right(45)

fuzzle.begin_fill()

fuzzle.forward(54)

fuzzle.left(40)

fuzzle.forward(150)

fuzzle.left(160)

fuzzle.forward(150)

fuzzle.left(40)

fuzzle.forward(54)

fuzzle.right(195)

fuzzle.end_fill()

count=count+1

fuzzle.left(360/number)