Introduction to Programming using Python 1st Edition

Published by Pearson
ISBN 10: 0132747189
ISBN 13: 978-0-13274-718-9

Chapter 6 - Functions - Programming Exercises - Page 212: 6.45

Answer

code

Work Step by Step

# 6.45 (Turtle: draw a regular polygon) Write the following function to draw a regular # polygon: # def drawPolygon(x = 0, y = 0, radius = 50, numberOfSides = 3): # The polygon is centered at (x, y) with a specified radius for the bounding circle for # the polygon and the number of sides. Write a test program that displays a triangle, # square, pentagon, hexagon, heptagon, and octagon, as shown in Figure 6.12a. import turtle def drawPolygon(x=0, y=0, radius=50, numberOfSides=3): turtle.penup() turtle.goto(x, y) turtle.pendown() turtle.circle(radius, steps=numberOfSides) def main(): drawPolygon(-200,50) # Triangle drawPolygon(-100, 50, numberOfSides=4) # Square drawPolygon(0, 50, numberOfSides=5) # Pentagon drawPolygon(100, 50, numberOfSides=6) # hexagon drawPolygon(200, 50, numberOfSides=7) # heptagon drawPolygon(300, 50, numberOfSides=8) # octagon turtle.done() main()
Update this answer!

You can help us out by revising, improving and updating this answer.

Update this answer

After you claim an answer you’ll have 24 hours to send in a draft. An editor will review the submission and either publish your submission or provide feedback.