Question

In: Computer Science

Python 3: Create functions to draw each of these with sides of size side:triangle, square, pentagon,...

Python 3:

Create functions to draw each of these with sides of size side:triangle, square, pentagon, hexagon
The function should be the shape + "2d"
Use import turtle and the turtle commands: forward(), left(), right(), pendown(), penup(), done(), color()
Use the goto(X,Y) function to allow the shape to be drawn at a specific location
Use the x,y, and side attributes for screen position and size

Solutions

Expert Solution

import turtle
#draw triangle
def draw_triangle(x,y,side):
   pen.penup()
   #set position to given location
   pen.setpos([x,y])
   pen.pendown()
   #forward by the side given
   #rotate the turtle pen to right by -120 degrees
   #if you want use positive angle then rotate left
   #iterate 3 times.
   for i in range(3):
       pen.forward(side)
       pen.right(-120)
   pen.penup()

#draw square
def draw_square(x,y,side):
   pen.penup()
   #set position to given location
   pen.setpos([x,y])
   pen.pendown()
   #forward by the side given
   #rotate the turtle pen to right by 90 degrees
   #iterate 4 times.
   for i in range(4):
       pen.forward(side)
       pen.right(90)
   pen.penup()

def draw_pentagon(x,y,side):
   pen.penup()
   #set position to given location
   pen.setpos([x,y])
   pen.pendown()
   #forward by the side given
   #rotate the turtle pen to left by 72 degrees
   #iterate 5 times.
   for i in range(5):
       pen.forward(side)
       pen.left(72)
   pen.penup()

def draw_hexagon(x,y,side):
   pen.penup()
   #set position to given location
   pen.setpos([x,y])
   pen.pendown()
   #forward by the side given
   #rotate the turtle pen to right by 60 degrees
   #iterate 6 times.
   for i in range(6):
       pen.forward(side)
       pen.right(60)
   pen.penup()
pen = turtle.Turtle()

#take input and call the drawing methods
x,y = map(int,input("Enter x and y positions of Triangle separated by space: ").strip().split(" "))
side = int(input("Enter side of Triangle(>50 for bigger shape): ").strip())
draw_triangle(x,y,side)
print()

x,y = map(int,input("Enter x and y positions of Square separated by space: ").strip().split(" "))
side = int(input("Enter side of Square(>50 for bigger shape): ").strip())
draw_square(x,y,side)

print()
x,y = map(int,input("Enter x and y positions of Pentagon separated by space: ").strip().split(" "))
side = int(input("Enter side of Pentagon(>50 for bigger shape): ").strip())
draw_pentagon(x,y,side)
print()
x,y = map(int,input("Enter x and y positions of Hexagon separated by space: ").strip().split(" "))
side = int(input("Enter side of Hexagon(>50 for bigger shape): ").strip())
draw_hexagon(x,y,side)
turtle.done()


Related Solutions

Please answer using python 3 and def functions! Lab 2 Drill 3: (function practice) create and...
Please answer using python 3 and def functions! Lab 2 Drill 3: (function practice) create and use a function named highest() that takes three inputs and returns the highest number. After you have got it working, try calling the function with inputs ‘hat’, ‘cat’, ‘rat’.
“Triangle Guessing” game in Python The program accepts the lengths of three (3) sides of a...
“Triangle Guessing” game in Python The program accepts the lengths of three (3) sides of a triangle as input . The program output should indicate if the triangle is a right triangle, an acute triangle, or an obtuse triangle. Make sure each side of the triangle is a positive integer. Use try/except for none positive integer input. validating the triangle. That is the sum of the lengths of any two sides of a triangle is greater than the length of...
Python: Lo Shu Magic Square The Lo Shu Magic Square is a grid with 3 rows...
Python: Lo Shu Magic Square The Lo Shu Magic Square is a grid with 3 rows and 3 columns, shown in figures below. The Lo Shu Magic Square has the following properties: The grid contains the numbers 1 through 9 exactly. The sum of each row, each column, and each diagonal all add up to the same number. This is shown in Figure B. In a program you can stimulate a magic square using a two-dimensional list. Write a function...
In Python Create customer information system as follows: Python 3 Create customer information system as follows:...
In Python Create customer information system as follows: Python 3 Create customer information system as follows: Ask the user to enter name, phonenumber, email for each customer. Build a dictionary of dictionaries to hold 10 customers with each customer having a unique customer id. (random number generated) Take the keys of the above mentioned dictionary which are customer ids and make a list. Ask the use to enter a customer id and do a binary search to find if the...
On python : d. Create a null vector of size 10 but the fifth value which...
On python : d. Create a null vector of size 10 but the fifth value which is 1. - 5 Point e. How to sum a small array faster than np.sum?- 5 Point
Create a Python program file and . Your program will draw random circles with filled and...
Create a Python program file and . Your program will draw random circles with filled and non-filled color (random colors) and rectangles, fill andnon-fill color (also random colors) on the canvas. At least 10 for each category
Why will a design for a Graeco-Latin square of size 3 not work well?
Why will a design for a Graeco-Latin square of size 3 not work well?
Create the following functions for an array in C++. Test with size 10, 10,000 and 100,000....
Create the following functions for an array in C++. Test with size 10, 10,000 and 100,000. Time each sort. Merge sort Insertion Sort Selection Sort         Bubble Sort Quick Sort PLEASE DO IT IN C++
Python 3 Forming Functions Define and complete the functions described below. * function name: say_hi *...
Python 3 Forming Functions Define and complete the functions described below. * function name: say_hi * parameters: none * returns: N/A * operation: just say "hi" when called. * expected output: >>> say_hi() hi * function name: personal_hi * parameters: name (string) * returns: N/A * operation: Similar to say_hi, but you should include the name argument in the greeting. * expected output: >>> personal_hi("Samantha") Hi, Samantha * function name: introduce * parameters: name1 (string) name2 (string) * returns: N/A...
Python 3 Functions that give answers Define and complete the functions described below. * function name:...
Python 3 Functions that give answers Define and complete the functions described below. * function name: get_name * parameters: none * returns: string * operation: Here, I just want you to return YOUR name. * expected output: JUST RETURNS THE NAME...TO VIEW IT YOU CAN PRINT IT AS BELOW >>> print(get_name()) John * function name: get_full_name * parameters: fname (string) lname (string) first_last (boolean) * returns: string * operation: Return (again, NOT print) the full name based on the first...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT