Question & Answer

Creating a flower with variable petals and colors

Creating a flower with variable petals and colors

For this activity you will use functions, loops, if statements, input statement and variables. You will also add comments to each section. Create a flower and stem. The flower will be dependent on user input for the number of petals, between 3 and 10. The petals will determine the color as well. Six petals or less will make the petals purple. Seven to 10 petals and the color is red.

One loop:
One input statement: petals
Two if statements:
Radius of circles: 200 (divide by petals)
Angle for flowers is: left(360/petals)

2 Answers

Verified archive
Quizzma TeamNovember 24, 2024

Sure! Here’s a simple Python program that achieves what you described:


import turtle # Import the turtle graphics library

# Ask user for input on the number of petals

petals = int(input("Enter the number of petals (between 3 and 10): "))

# Check if the number of petals is within the valid range

if 3 <= petals <= 10:

# Determine petal color based on the number of petals

if petals <= 6:

color = "purple" # Set color to purple for 6 or fewer petals

else:

color = "red" # Set color to red for 7 to 10 petals

# Calculate radius and angle based on the number of petals

radius = 200 / petals

angle = 360 / petals

# Create the flower using loops

turtle.color(color) # Set the turtle color

for _ in range(petals): # Loop for the number of petals

turtle.circle(radius) # Draw a petal

turtle.left(angle) # Rotate the turtle for the next petal

turtle.done() # Complete the drawing

else:

print("Please enter a number between 3 and 10.")

### Explanation:
1. Input Statement: The program starts by asking the user to input the number of petals they want, which must be between 3 and

AnonymousNovember 24, 2024

import turtle

# Function to draw the flower
def draw_flower(petals):
angle = 360 / petals # Calculate the angle for the petals
radius = 200 / petals # Calculate the radius of each petal

# Set the petal color based on the number of petals
if petals <= 6:
turtle.color(“purple”)
else:
turtle.color(“red”)

turtle.begin_fill()
for _ in range(petals):
turtle.circle(radius, 60) # Draw the first half of the petal
turtle.left(120) # Turn to draw the other half
turtle.circle(radius, 60)
turtle.left(360 / petals) # Adjust for the next petal
turtle.end_fill()

# Function to draw the stem
def draw_stem():
turtle.color(“green”)
turtle.penup()
turtle.goto(0, -200) # Move to the bottom center of the flower
turtle.pendown()
turtle.setheading(270) # Point downwards
turtle.forward(200) # Draw the stem

# Main program
def main():
turtle.speed(5)
petals = int(input(“Enter the number of petals (between 3 and 10): “))

# Ensure the user inputs a valid number of petals
if 3 <= petals <= 10:
draw_flower(petals)
draw_stem()
else:
print(“Please enter a number between 3 and 10.”)

turtle.done()

# Run the program
main()