class HexagonInteriorAngle(object):
def __init__(self, x):
self.x = self
def FindInteriorAngle(self):
degrees = int((x - 2) * 180)
interior = int(degrees / x)
def Print(self):
if x == 3:
print(str("an interior angle of a triangle equals " str(interior)))
elif x == 4:
print("an interior angle of an equilateral equals " str(interior))
elif x == 5:
print("an interior angle of a pentagon equals " str(interior))
elif x == 6:
print("an interior angle of a hexagon equals " str(interior))
elif x == 7:
print("an interior angle of a heptagon equals " str(interior))
elif x == 8:
print("an interior angle of an octagon equals " str(interior))
elif x == 9:
print("an interior angle of a nonagon equals " str(interior))
elif x == 10:
print("an interior angle of a decagon equals " str(interior))
else:
print(str(interior))
if __name__ == "__main__":
x = int(input("enter: "))
hexaObj = HexagonInteriorAngle(x)
hexaObj.FindInteriorAngle()
hexaObj.Print()
what i want to program to do is identify what type of polygon it is based off of the number of sides (ex. 6 sides = hexagon, 5 sides = pentagon, etc) and then print what one interior angle would be for that polygon (formula to find the interior angle : (the number of sides - 2) x 180 and then taking that answer and then dividing it by the number of sides). example: hexagon. ( 6 - 2 ) x 180 = 720 720 / 6 = 120
right now i'm pretty sure the actual code part is correct because if you do this it prints fine:
class HexagonInteriorAngle(object):
def __init__(self, x):
self.x = self
def FindInteriorAngle(self):
degrees = int((x - 2) * 180)
interior = int(degrees / x)
print("interior angle " str(interior))
if __name__ == "__main__":
x = int(input("enter: "))
hexaObj = HexagonInteriorAngle(x)
hexaObj.FindInteriorAngle()
CodePudding user response:
You should do this:
def FindInteriorAngle(self):
degrees = int((x - 2) * 180)
interior = int(degrees / x)
return interior
# ...
if __name__ == "__main__":
x = int(input("enter: "))
hexaObj = HexagonInteriorAngle(x)
interior = hexaObj.FindInteriorAngle()
print("interior angle " str(interior))
CodePudding user response:
Almost unrelated to your answer, we could clean up your code a bit with the use of some property
fields. Using __str__
instead of a Print
method is more idiomatic, as well.
class Polygon:
shapes = [
None, # index 0
None, # index 1
"line", # index 2, etc...
"triangle",
"rectangle",
"pentagon",
"hexagon",
"heptagon",
"octogon",
"nonagon",
"decagon",
]
def __init__(self, sides):
if not 2 <= sides <= 10:
raise ValueError("Polygon only supports shapes with sides 2-10")
self.sides = sides
@property
def shape(self):
return self.shapes[self.sides]
@property
def interior_angle(self):
return (self.sides - 2) * 180 / self.sides
def __str__(self):
return f"The interior angle of a {self.shape} is {self.interior_angle}"
if __name__ == "__main__":
poly = Polygon(3)
print(poly)