1
1
Fork 0

updated repository with strict typechecking
Some checks failed
Actions / Lint Code (Ruff & Pylint) (push) Failing after 11s

This commit is contained in:
SeaswimmerTheFsh 2024-05-08 12:43:05 -04:00
parent 14c41cfd45
commit 68569900e0
Signed by: cswimr
GPG key ID: 5D671B5D03D65A7F
5 changed files with 30 additions and 24 deletions

View file

@ -5,29 +5,26 @@ 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
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
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
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 = input(
x: str = input(
f"{BLUE}Input a number to divide:{RESET} "
) # Allows us to input a number for X to divide
y = input(
y: str = 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:
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}")