diff options
| author | yctct <yctct@yctct.com> | 2026-06-07 08:59:04 +0200 |
|---|---|---|
| committer | yctct <yctct@yctct.com> | 2026-06-07 08:59:04 +0200 |
| commit | 15115b4c52bfda0d1cca9fa1155beecbb873ec35 (patch) | |
| tree | b3f0975e63eb04dcba732a78ce9bd9abda8acf01 /py03/ex2/ft_coordinate_system.py | |
First commit, add all files
Diffstat (limited to 'py03/ex2/ft_coordinate_system.py')
| -rwxr-xr-x | py03/ex2/ft_coordinate_system.py | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/py03/ex2/ft_coordinate_system.py b/py03/ex2/ft_coordinate_system.py new file mode 100755 index 0000000..f0daea1 --- /dev/null +++ b/py03/ex2/ft_coordinate_system.py @@ -0,0 +1,61 @@ +#!/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) |
