Last active
February 13, 2021 09:41
-
-
Save StepanPakholok/1c675e46c40153a61bf0ad038ce27338 to your computer and use it in GitHub Desktop.
triangle calculation (ukrainian, python learhing, school)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math | |
# --- Strings --- | |
SELECT_TASK_MESSAGE = ''' | |
Виберіть тип вирішення (1 або 2): | |
1: невідомий катет; | |
2: невідома гіпотенуза; | |
''' | |
TASK_SELECTED_MESSAGE = 'Вибрано: ' | |
INPUT_TRIANGLE_LEG_MESSAGE = 'Введіть довжину катета (число): ' | |
INPUT_TRIANGLE_HYPOTENUSE_MESSAGE = 'Введіть довжину гіпотенузи (число): ' | |
RESULT_MESSAGE = 'Результат обчислення: ' | |
TASK_SELECTION_ERROR = 'Помилка! Невірний вибір.' | |
DATA_INPUT_ERROR = 'Помилка! Катет не може бути більшим або рівним гіпотенузи. Або введено невірні дані' | |
# ----- Functions ----- | |
def select_task(): | |
selected_task = input(SELECT_TASK_MESSAGE) | |
if selected_task == '1' or selected_task == '2': | |
print(TASK_SELECTED_MESSAGE + selected_task) | |
return selected_task | |
else: | |
exit(TASK_SELECTION_ERROR) | |
def calculate_triangle_leg(): | |
hypotenuse = float(input(INPUT_TRIANGLE_HYPOTENUSE_MESSAGE)) | |
leg_1 = float(input(INPUT_TRIANGLE_LEG_MESSAGE)) | |
if leg_1 <= 0 or hypotenuse <= 0 or hypotenuse <= leg_1: | |
exit(DATA_INPUT_ERROR) | |
leg_2 = math.sqrt(hypotenuse ** 2 - leg_1 ** 2) | |
return leg_2 | |
def calculate_hypotenuse(): | |
leg_1 = float(input(INPUT_TRIANGLE_LEG_MESSAGE)) | |
leg_2 = float(input(INPUT_TRIANGLE_LEG_MESSAGE)) | |
if leg_1 <= 0 or leg_2 <= 0: | |
exit(DATA_INPUT_ERROR) | |
hypotenuse = math.sqrt(leg_1 ** 2 + leg_2 ** 2) | |
return hypotenuse | |
# ----- Main ----- | |
task = select_task() | |
if task == '1': | |
result = calculate_triangle_leg() | |
elif task == '2': | |
result = calculate_hypotenuse() | |
print(RESULT_MESSAGE + str(result)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment