Advertisement
Spocoman

07. Area of Figures

Dec 10th, 2021 (edited)
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.52 KB | None | 0 0
  1. from math import pi
  2.  
  3. figure = input()
  4. area = 0
  5.  
  6. if figure == 'square':
  7.     side = float(input())
  8.     area = side * side
  9.  
  10. elif figure == 'triangle':
  11.     lenght = float(input())
  12.     hight = float(input())
  13.     area = lenght * hight / 2
  14.  
  15. elif figure == 'circle':
  16.     radius = float(input())
  17.     area = radius * radius * pi
  18.  
  19. elif figure == 'rectangle':
  20.     first_side = float(input())
  21.     second_side = float(input())
  22.     area = first_side * second_side
  23.  
  24. print(f'{area:.3f}')
  25.  
  26.  
  27. ДРУГО РЕШЕНИЕ:
  28.  
  29. from math import pi
  30.  
  31. figure = input()
  32.  
  33. if figure == 'square':
  34.     print(f'{float(input()) ** 2:.3f}')
  35. elif figure == 'triangle':
  36.     print(f'{float(input()) * float(input()) / 2:.3f}')
  37. elif figure == 'circle':
  38.     print(f'{float(input()) ** 2 * pi:.3f}')
  39. elif figure == 'rectangle':
  40.     print(f'{float(input()) * float(input()):.3f}')
  41.  
  42.  
  43. РЕШЕНИЕ С ТЕРНАРЕН ОПЕРАТОР:
  44.  
  45. from math import pi
  46.  
  47. figure = input()
  48.  
  49. area = float(input()) ** 2 if figure == 'square' \
  50.     else float(input()) ** 2 * pi if figure == 'circle' \
  51.     else float(input()) * float(input()) / 2 if figure == 'triangle' \
  52.     else float(input()) * float(input()) if figure == 'rectangle' else 0
  53.  
  54. print(f'{area:.3f}')
  55.  
  56. РЕШЕНИЕ С IF-ELSE И ТЕРНАРЕН ОПЕРАТОР:
  57.  
  58. from math import pi
  59.  
  60. figure = input()
  61.  
  62. if figure in ('square', 'circle'):
  63.     print(f'{float(input()) ** 2 * (pi if figure == "circle" else 1):.3f}')
  64. elif figure in ('triangle', 'rectangle'):
  65.     print(f'{float(input()) * float(input()) / (2 if figure == "triangle" else 1):.3f}')
  66.  
  67.  
  68. РЕШЕНИЕ С РЕЧНИК:
  69.  
  70. from math import pi
  71.  
  72. figure = input()
  73. a = float(input())
  74. b = 0
  75. if figure in ('triangle', "rectangle"):
  76.     b = float(input())
  77.  
  78. area = {'square': a ** 2,
  79.         'triangle': a * b / 2,
  80.         'circle': a ** 2 * pi,
  81.         'rectangle': a * b
  82.         }
  83.  
  84. print(f'{area[figure]:.3f}' if figure in area else '')
  85.  
  86.  
  87. РЕШЕНИЕ С ФУНКЦИИ:
  88.  
  89. from math import pi
  90.  
  91.  
  92. def figures(figure):
  93.     if figure == 'square':
  94.         return square()
  95.     elif figure == 'triangle':
  96.         return triangle()
  97.     elif figure == 'circle':
  98.         return circle()
  99.     elif figure == 'rectangle':
  100.         return rectangle()
  101.  
  102.  
  103. def square():
  104.     return float(input()) ** 2
  105.  
  106.  
  107. def triangle():
  108.     return float(input()) * float(input()) / 2
  109.  
  110.  
  111. def circle():
  112.     return float(input()) ** 2 * pi
  113.  
  114.  
  115. def rectangle():
  116.     return float(input()) * float(input())
  117.  
  118.  
  119. print(f'{figures(input()):.3f}')
  120.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement