#!/usr/bin/env python3 import math class InvalidSyntax(Exception): def __init__(self, message="Invalid syntax"): self.message = message super().__init__(self.message) def get_player_pos(): while True: try: pos = input("Enter new coordinates" "as floats in format 'x, y, z': ") split_pos = pos.split(',') if len(split_pos) != 3: raise InvalidSyntax() try: for element in split_pos: float(element) except ValueError: print(f"Error on parameter '{element}':" f"could not convert string to float '{element}'") # break because of True loop else: break except InvalidSyntax as err: print(err) return pos def print_info_pos_one(pos): split_pos: tuple[float, ...] = tuple((float(x) for x in pos.split(","))) (x1, y1, z1) = split_pos print(f"It includes: X={x1}, Y={y1}, Z={z1}") x2 = y2 = z2 = 0 distance_to_center = math.sqrt((x2-x1)**2 + (y2-y1)**2 + (z2-z1)**2) print(f"Distance to the center: {round(distance_to_center, 4)}") def compute_distance_btw_pos(str_one, str_two): pos_one: tuple[float, ...] = tuple((float(x) for x in str_one.split(","))) (x1, y1, z1) = pos_one pos_two: tuple[float, ...] = tuple((float(x) for x in str_two.split(","))) (x2, y2, z2) = pos_two distance_btw_two = math.sqrt((x2-x1)**2 + (y2-y1)**2 + (z2-z1)**2) print(f"Distance between the two sets of coordinates: " f"{round(distance_btw_two, 4)}") if __name__ == "__main__": print("=== Game Coordinate System ===") pos_one = get_player_pos() print(f"Got a first tuple: ({pos_one})") print_info_pos_one(pos_one) print() print("Get a second set of coordinates") pos_two = get_player_pos() compute_distance_btw_pos(pos_one, pos_two)