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...
create a new Python 3 Jupyter Notebook.. Create one python code cell for the problem below....
create a new Python 3 Jupyter Notebook.. Create one python code cell for the problem below. Use any additional variables and comments as needed to program a solution to the corresponding problem. All functions should be defined at the top of the code in alphabetical order. Problem: Define a function with the following characteristics: The function's purpose will be checking that a number matches another, so it should be named appropriately. It must accept one parameter, which is the number...
Create a new Python 3 Jupyter Notebook. Create one python code cell for the problem below....
Create a new Python 3 Jupyter Notebook. Create one python code cell for the problem below. Use any additional variables and comments as needed to program a solution to the corresponding problem. All functions should be defined at the top of the code in alphabetical order. When done, download the ipynb file and submit it to the appropriate dropbox in the course's canvas page. Problem: Define a function with the following characteristics: The function's purpose will be calculating a factORRial...
Create a new Python 3 Jupyter Notebook. Create one python code cell for the problem below....
Create a new Python 3 Jupyter Notebook. Create one python code cell for the problem below. Use any additional variables and comments as needed to program a solution to the corresponding problem. All functions should be defined at the top of the code in alphabetical order. *Problem:* 1. Define a function with the following characteristics: * The function's purpose will be checking that a number matches another, so it should be named appropriately. * It must accept one parameter, which...
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?
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 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++
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT