简体   繁体   中英

How to print in Python using class and print function

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 the program to do is to 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()

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))

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM