Правильно было бы отделить друг от друга интерфейс взаимодействия с пользователем от калькулятора.
import math
class Distance:
def __init__(self, lat, long):
self._lat = math.radians(lat)
self._long = math.radians(long)
@property
def lat(self):
return self._lat
@property
def long(self):
return self._long
def __repr__(self):
return f"latitude - {self._lat} longitude - {self._long}"
class DistanceCalculator:
K_CONST = 6371.01
def calculate(self, point_a: Distance, point_b: Distance):
return self.K_CONST * math.acos(
math.sin(point_a.lat) * math.sin(point_b.lat) +
math.cos(point_a.lat) * math.cos(point_b.lat) *
math.cos(point_a.long - point_b.long)
)
class GUI:
ACCEPTED_ACTIONS = {1: "Рассчёт дистанции", 2: "Выход"}
ACCEPTED_ACTIONS_STR = "\n".join([f"{k}: {v}" for k, v in ACCEPTED_ACTIONS.items()])
def main(self):
while True:
action = input(f'Выберите действие:\n{self.ACCEPTED_ACTIONS_STR}\n')
try:
int_act = int(action)
if int_act not in self.ACCEPTED_ACTIONS:
raise ValueError('Недопустимый пункт меню')
if int_act == 1:
a = self.get_distance("A")
b = self.get_distance("B")
calc = DistanceCalculator()
distance = calc.calculate(a, b)
print(f'Distance between {a} and {b} is {distance}')
elif int_act == 2:
print('Выход')
break
except (TypeError, ValueError) as E:
print(E)
def get_distance(self, point: str):
latitude = float(input(f'Enter the latitude point {point}: '))
longitude = float(input(f'Enter the longitude point {point}: '))
return Distance(latitude, longitude)
if __name__ == "__main__":
gui = GUI()
gui.main()