Write a Python program that draws a circle, triangle, square and octagon using Turtle graphics. The program will prompt the user how many sides each shape has. The valid sizes are: 0 – for circle 3 – for triangle 4 – for square 8 – for octagon When the user selects a number from the options above, the application will draw the shapes. Each shape is to be implemented using a method. You will also implement a main method that demonstrates each of the methods that draws each shape. If the user selects an invalid option, the program must provide an invalid response and prompt the user to enter another selection. There will be five methods in total. For example, if the user selects 0 which is the option to draw a circle, the program must prompt the user for the radius. The radius will be used to draw the circle. The program will prompt the user if they would like to add an additional shape. If they choose “Yes”, the program will prompt the user for the amount of sides and draw another shape. The program must clear the canvas before drawing any new shapes. If the user selects nothing else, the program will stop Write a Python program that draws a circle, triangle, square and octagon using Turtle graphics. The program will prompt the user how many sides each shape has. The valid sizes are: 0 – for circle 3 – for triangle 4 – for square 8 – for octagon When the user selects a number from the options above, the application will draw the shapes. Each shape is to be implemented using a method. You will also implement a main method that demonstrates each of the methods that draws each shape. If the user selects an invalid option, the program must provide an invalid response and prompt the user to enter another selection. There will be five methods in total. For example, if the user selects 0 which is the option to draw a circle, the program must prompt the user for the radius. The radius will be used to draw the circle. The program will prompt the user if they would like to add an additional shape. If they choose “Yes”, the program will prompt the user for the amount of sides and draw another shape. The program must clear the canvas before drawing any new shapes. If the user selects nothing else, the program will stop.
The Correct Answer and Explanation is :
Here’s a Python program that uses the Turtle graphics library to draw a circle, triangle, square, and octagon. The user selects how many sides the shape should have, and based on that input, the program will draw the appropriate shape. The program also clears the canvas between drawing shapes and prompts the user if they’d like to draw more.
import turtle
# Method to draw a circle
def draw_circle():
radius = int(turtle.numinput("Circle", "Enter the radius of the circle:", minval=1))
turtle.penup()
turtle.goto(0, -radius) # Move turtle to starting point
turtle.pendown()
turtle.circle(radius)
# Method to draw a triangle
def draw_triangle():
for _ in range(3):
turtle.forward(100)
turtle.left(120)
# Method to draw a square
def draw_square():
for _ in range(4):
turtle.forward(100)
turtle.left(90)
# Method to draw an octagon
def draw_octagon():
for _ in range(8):
turtle.forward(50)
turtle.left(45)
# Main method to control the flow of the program
def main():
turtle.speed(5)
while True:
turtle.reset() # Clear the canvas
choice = int(turtle.numinput("Shape Selector", "Enter the number of sides (0 for circle, 3 for triangle, 4 for square, 8 for octagon):"))
if choice == 0:
draw_circle()
elif choice == 3:
draw_triangle()
elif choice == 4:
draw_square()
elif choice == 8:
draw_octagon()
else:
turtle.write("Invalid choice. Please select a valid number.", align="center", font=("Arial", 12, "normal"))
continue
another = turtle.textinput("Draw Another?", "Would you like to add another shape? (Yes/No)").lower()
if another != 'yes':
break
turtle.done()
if __name__ == "__main__":
main()
Explanation:
- Methods for Drawing Shapes:
- draw_circle(): Prompts the user for the radius and draws a circle with that radius. The turtle is moved to the appropriate starting position before drawing.
- draw_triangle(): Uses a loop to draw a triangle by moving the turtle forward and turning 120 degrees after each side.
- draw_square(): Similar to the triangle, but with 90-degree turns to form a square.
- draw_octagon(): Draws an octagon by turning the turtle 45 degrees after each of the 8 sides.
- Main Function:
- The program enters a loop where it asks the user for input on how many sides the shape should have. Based on this input, the corresponding shape is drawn.
- If the user inputs an invalid option (not 0, 3, 4, or 8), the program shows an error message and prompts for input again.
- After each shape is drawn, the user is asked if they want to draw another shape. If the user types “Yes”, the program prompts for the number of sides again and draws a new shape.
- The canvas is cleared before each new shape is drawn to ensure it does not overlap with previous drawings.
- Canvas Reset:
turtle.reset()is used to clear the canvas before each new shape is drawn, ensuring that the new shape doesn’t overlap with the previous one.
- User Interaction:
- The program uses
turtle.numinput()andturtle.textinput()to interact with the user for input regarding the shape to draw and whether to continue drawing more shapes.
- The program uses
This program provides an interactive drawing experience where the user can create multiple shapes and control the flow of the drawing process.