2024-04-29 19:53:35 -04:00
|
|
|
# ANSI color codes
|
2024-04-29 20:00:23 -04:00
|
|
|
BLUE = "\033[34m" # Blue
|
|
|
|
RED = "\033[31m" # Red
|
|
|
|
GREEN = "\033[32m" # Green
|
2024-04-29 20:14:53 -04:00
|
|
|
RESET = "\033[0m" # Reset to default color
|
|
|
|
|
2024-04-29 19:53:35 -04:00
|
|
|
|
2024-05-08 12:43:05 -04:00
|
|
|
def divide(x: float, y: float) -> float:
|
|
|
|
x = float(x) # Tries to convert the x argument into a float
|
|
|
|
y = float(y) # tries to convert the y argument into a float
|
2024-04-29 19:53:35 -04:00
|
|
|
|
2024-05-06 20:02:57 -04:00
|
|
|
if y == 0: # This argument checks to see if the inputted y is a 0
|
2024-05-08 12:43:05 -04:00
|
|
|
raise ZeroDivisionError # If the Y does check out to be a zero, we raise an error that you cannot divide by zero
|
2024-04-29 20:14:53 -04:00
|
|
|
return float(x / y)
|
|
|
|
|
2024-04-29 19:53:35 -04:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2024-05-08 12:43:05 -04:00
|
|
|
x: str = input(
|
2024-05-06 20:02:57 -04:00
|
|
|
f"{BLUE}Input a number to divide:{RESET} "
|
|
|
|
) # Allows us to input a number for X to divide
|
2024-05-08 12:43:05 -04:00
|
|
|
y: str = input(
|
2024-05-06 20:02:57 -04:00
|
|
|
f"{BLUE}Input a number to divide by:{RESET} "
|
|
|
|
) # Allows us to input a number for Y to divide x by
|
2024-05-08 12:43:05 -04:00
|
|
|
try:
|
|
|
|
result: float = divide(float(x), float(y))
|
2024-04-29 20:14:53 -04:00
|
|
|
print(f"{GREEN}{result}{RESET}")
|
2024-05-08 12:43:05 -04:00
|
|
|
except (ZeroDivisionError, ValueError) as e:
|
|
|
|
if isinstance(e, ZeroDivisionError):
|
|
|
|
print(f"{RED}You cannot divide by zero!{RESET}")
|
|
|
|
print(f"{RED}Please do not input strings!{RESET}")
|