updated repository with strict typechecking
Some checks failed
Actions / Lint Code (Ruff & Pylint) (push) Failing after 11s
Some checks failed
Actions / Lint Code (Ruff & Pylint) (push) Failing after 11s
This commit is contained in:
parent
14c41cfd45
commit
68569900e0
5 changed files with 30 additions and 24 deletions
|
@ -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}")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue