Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Python Assignment: Please help 6 Programming Challenges: Geometry/Drawing o Writ

ID: 3814677 • Letter: P

Question

Python Assignment: Please help

6 Programming Challenges: Geometry/Drawing o Write a function that takes 1 number, subtracts 2, and prints the result. o Write a function that takes in the number of sides, calculates the sum of the interior angles of a polygon (n-2) 180, and prints the result. o Write a function that takes in the number of sides, calculates the interior angle of a regular polygon o Write a function that calculates the exterior angle of a polygon. You'll want to subtract the interior angle from 180 o Write a function that takes in the number of sides, a length for each side, calculates the interior angle of a regular polygon, then draws the polygon. You will need to import the turtle library. You can use turtle left(angle) or turtle.right(angle) to turn and turtle.forward(steps) to move forward o Fix the function so the window sticks around. You'll need to use win -turtle.Screen0 at the beginning of the program so you have a way to refer to the window (the variable win). Then at the end of the program you can use win.exitonclick0 to keep the window open until you click on it. That way you can make sure the drawing worked. o Write a user interface that prompts the user for a number of sides then a length. Call your function once the user enters the number of sides and length. o Play with bad user inputs. Figure out where it breaks and what error messages you get. Add try: except: blocks o Modify your user interface using while loops. Instead of stopping and quitting when the user

Explanation / Answer

def subtractFrom2(num):
return num-2

def sumOfInteriorAngles(sides):
return (sides-2)*180

def findInteriorAngle(sumOfInteriors,sides):
return sumOfInteriors/sides

def findExteriorAngle(interiorAngle):
return (sides-2)*180

def drawPolygon(sides,length):
angle = 180-int(findInteriorAngle(sumOfInteriorAngles(sides),sides))
while (sides>=0):
turtle.forward(length)
turtle.left(angle)
turtle.forward(length)
turtle.left(angle)
sides = sides-2

import turtle
import sys
win = turtle.Screen()

try:

number = int(input("Enter number to subtract from 2 : "))
print("Result is : ",subtractFrom2(int(number)))
sides = int(input("Enter number of sides to calculate sum of interior angles : "))
sumofInteriors = int(sumOfInteriorAngles(sides))
print("Sum of interior angles is : ", sumofInteriors)
interiorAngle = int(findInteriorAngle(sumofInteriors,sides))
print("Interior angle is : ", interiorAngle)
exteriorAngle = int(180 - interiorAngle)
print("Exterior Angle is : ", exteriorAngle)
length = int(input("Enter length of each side of polygon: "))

drawPolygon(sides,length)
win.exitonclick()

f = open('myfile.txt')
s = f.readline()
i = int(s.strip())

except:
print("Error:", sys.exc_info()[0])