Calculating area of different figures on a case by case basis My program looks for the area of ​​the figures, depending on the value of the variable а.

area -&gt; triangle

area -&gt; triangle2

area -&gt; square

area -&gt; rhombus
I want to get constructive criticism about the structure of the code and what can be redone. The program is written in the Python programming language.


while True:
    class S():
        print(&quot;Для выхода нажмите Enter&quot;)
        a = int(input(&quot;1. Площадь прямоугольного тр. \n2. Площадь равнобедренного тр. \n3. Площадь квадрата \n4. Площадь ромба  &quot;))
        if a == 1:
            print(&quot;Это нахождение площади прямоугольного треугольника&quot;)
            a1 = float(input(&quot;Введите первый катет  &quot;))
            b1 = float(input(&quot;Введите второй катет  &quot;))
            class Triangle1():
                    def  __init__(self, a1, b1):
                        self.a = a1
                        self.b = b1
                    def area(self):
                        return (self.a * self.b) / 2
            Striangle1 = Triangle1(a1,b1)        
            print(&quot;Площадь равна = &quot;,Striangle1.area())
        elif a == 2:
            print(&quot;Это нахождение площади равнобедренного треугольника&quot;)
            a2 = float(input(&quot;Введите основание  &quot;))
            b2 = float(input(&quot;Введите высоту  &quot;))
            class Triangle2():
                    def  __init__(self, a2, b2):
                        self.a2 = a2
                        self.b2 = b2
                    def area(self):
                        return (self.a2/2)*self.b2
            Striangle2 = Triangle2(a2,b2)        
            print(&quot;Площадь равна = &quot;,Striangle2.area())
        if a == 3:
            print(&quot;Это нахождение площади квадрата  &quot;)
            a3 = float(input(&quot;Введите сторону  &quot;))
            class Hexagon():
                def __init__(self, a3):    
                    self.a3 = a3
                def area(self):
                    return self.a3**2
            Shexagon = Hexagon(a3)
            print(&quot;Площадь равна = &quot;,Shexagon.area())            
        elif a == 4:
            print(&quot;Это нахождение площади ромба&quot;)
            a4 = float(input(&quot;Введите 1 диагональ  &quot;))
            b4 = float(input(&quot;Введите 2 диагональ  &quot;))
            class Romb():
                def  __init__(self, a4, b4):
                    self.a4 = a4
                    self.b4 = b4
                def area(self):
                    return (self.a4*self.b4)/2
            Sromb = Romb(a4,b4)        
            print(&quot;Площадь равна = &quot;,Sromb.area())        

Drive link to the code
First, note that finding the area of an isosceles triangle is the same formula as finding the area of right triangle.  Both are 1/2 * base * height.  

You are mixing object oriented classes and imperative programming.  This usually happens as you are first learning about classes.  Here is are two alternate structures to your code:

First option, you can ignore classes.   The only commonality between a triangle and an octagon is that both have an area() method.   There is no common data: a triangle has no radius and an octagon has nothing but a radius.  You can make functions like triangle_area(base, height), octagon_radius(radius).  Both return areas.  Just doing this would simplify your code.

Second option, you use classes anyway.  You might do this if you expect more commonality in the near future.  For example, if this is becoming a drawing program then a Figure might be a class that "has a x,y center, current color, a move command, a draw command and an area function."  This code would have code common to all figures in class Figure and would have empty (or virtual) routines that are 'parts any subclass needs to implement to considered a Figure'.   

Here is some a code outline to ponder:

 class Figure:
     def area(self):  # return the area of this figure
         raise NotImplementedExeception('subclass must have an area() function')

 class Triangle(Figure):   # inheritence is the promise to follow the superclass API
     def __init__(self, base, height):
         super().__init__()   # this is boilerplate
         self.base, self.height = base, height

     def area(self):
         return self.base * self.height / 2

     @classmethod  # used for custom constructors
     def ask_right(cls):
        print("Это нахождение площади прямоугольного треугольника")
        a1 = float(input("Введите первый катет  "))
        b1 = float(input("Введите второй катет  "))
        return cls(a1, a2)   # return a new triangle

     @classmethod
     def ask_isosceles():
        ...

def main():
        print("Для выхода нажмите Enter")
        a = int(input("1. Площадь прямоугольного тр. \n2. Площадь равнобедренного тр. \n3. Площадь квадрата \n4. Площадь ромба  "))
        if a == 1:
            fig = Triangle.ask_right()
        elif a == 2:
            fig = Triangle.ask_isosceles()
        elif a == 3:
            fig = Octagon.ask()

        print f"Area is {fig.area()}")


In summary, using classes to organize your code makes more sense if you are doing more than just one item.   The real power comes from being able to write functions like:

def redraw(list_of_figures):
    clear_screen()
    for fig in list_of_figures:
        fig.draw()   # which might be Circle.draw(), Triangle.draw() or another subclass


and adding a new subclass, say Pentagon(Figure), without having to change the redraw() function.

Classes can be hard to get your head around; that is normal.

Keep hacking!  Keep notes.