import sys
import time
from math import sqrt
def visual_effects(f):
def wrapper(*args, **kwargs):
print('The result is being calculated, please wait...')
time.sleep(2)
res = f(*args, **kwargs)
print(f'The area of the triangle is: {res} cm.')
return wrapper
@visual_effects
def area_of_triangle(side1, side2, side3):
match side1 and side2 and side3:
case 0:
print('Error! All sides of the triangle must be greater than zero.')
sys.exit()
perimeter = (side1 + side2 + side3) / 2
result = round(sqrt(perimeter * (perimeter - side1) * (perimeter - side2) * (perimeter - side3)), 2)
return result
try:
area_of_triangle(float(input('Enter the length of side of triangle 1 (cm): ')),
float(input('Enter the length of side of triangle 2 (cm): ')),
float(input('Enter the length of side of triangle 3 (cm): ')))
except ValueError:
print('Error! Please enter an integer or fractional number for the program to work correctly.')
Погуглив нашел только вариант, где мы оборачиваем всю нашу программу в функцию и при согласии пользователя, повторяем выполнение этой функции. Есть ли другие варианты?
import time
from math import sqrt
def visual_effects(f):
def wrapper(*args, **kwargs):
print('The result is being calculated, please wait...')
time.sleep(2)
res = f(*args, **kwargs)
print(f'The area of the triangle is: {res} cm.')
return wrapper
@visual_effects
def area_of_triangle(side1, side2, side3):
if side1 <= 0 or side2 <= 0 or side3 <= 0:
print('Error! All sides of the triangle must be greater than zero.')
return None
perimeter = (side1 + side2 + side3) / 2
result = round(sqrt(perimeter * (perimeter - side1) * (perimeter - side2) * (perimeter - side3)), 2)
return result
# бесконечный цикл, пока пользователь его не прервет
while True:
try:
side1 = float(input('Enter the length of side of triangle 1 (cm): '))
side2 = float(input('Enter the length of side of triangle 2 (cm): '))
side3 = float(input('Enter the length of side of triangle 3 (cm): '))
except ValueError:
print('Error! Please enter an integer or fractional number for the program to work correctly.')
continue
area_of_triangle(side1, side2, side3)
user_input = input('Repeat calculate? (yes/no): ').lower()
if user_input != 'yes':
break # Прерываем цикл, если пользователь ответил != yes
# Функция для подставления номера стороны и номера треугольника для отображения
def enter_length(side_num: str, triangle_num: str):
return f'Enter the length of {side_num} side of triangle {triangle_num} (cm): '
error_msg = 'Error! Please enter an integer or fractional number for the program to work correctly.'
repeat_msg = "Enter 'Y' for one more triangle or any key to exit: "
# Номер треугольника
triangle_num = 1
while True:
try:
sides = []
# Номер стороны из 3
for side_num in range(1, 4):
sides.append(
float(input(enter_length(str(side_num), str(triangle_num)))))
area_of_triangle(*sides)
except ValueError:
print(error_msg)
else:
response = input(repeat_msg)
if response == 'Y':
triangle_num += 1
else:
break