1
1
Fork 0
PythonLearning/division/main.py
EntropicDecay 232cc60d19 Added notes to it
Co-authored-by: seaswimmerthefsh@gmail.com <seaswimmerthefsh@gmail.com>
2024-05-06 19:02:57 -05:00

33 lines
1.3 KiB
Python

# ANSI color codes
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 | None:
try: # This function checks to see if we can convert the input into a 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
except ValueError:
return print(
f"{RED}You cannot divide by strings!{RESET}"
) # if the input was unable to be turned into a float, it prints out an error stating that you cannot print out strings
if y == 0: # This argument checks to see if the inputted y is a 0
return print(
f"{RED}You cannot divide by 0!{RESET}"
) # If the Y does check out to be a zero, we print out the error that you cannot divide by zero
return float(x / y)
if __name__ == "__main__":
x = input(
f"{BLUE}Input a number to divide:{RESET} "
) # Allows us to input a number for X to divide
y = input(
f"{BLUE}Input a number to divide by:{RESET} "
) # Allows us to input a number for Y to divide x by
result = divide(x, y)
if result is not None:
print(f"{GREEN}{result}{RESET}")