1
1
Fork 0
PythonLearning/division/main.py

31 lines
1.1 KiB
Python
Raw Normal View History

# ANSI color codes
2024-04-29 20:00:23 -04:00
BLUE = "\033[34m" # Blue
RED = "\033[31m" # Red
GREEN = "\033[32m" # Green
RESET = "\033[0m" # Reset to default color
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
if y == 0: # This argument checks to see if the inputted y is a 0
raise ZeroDivisionError # If the Y does check out to be a zero, we raise an error that you cannot divide by zero
return float(x / y)
if __name__ == "__main__":
x: str = input(
f"{BLUE}Input a number to divide:{RESET} "
) # Allows us to input a number for X to divide
y: str = input(
f"{BLUE}Input a number to divide by:{RESET} "
) # Allows us to input a number for Y to divide x by
try:
result: float = divide(float(x), float(y))
print(f"{GREEN}{result}{RESET}")
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}")