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:
Seaswimmer 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

6
.vscode/settings.json vendored Normal file
View file

@ -0,0 +1,6 @@
{
"python.analysis.inlayHints.functionReturnTypes": true,
"python.analysis.inlayHints.pytestParameters": true,
"python.analysis.inlayHints.variableTypes": true,
"python.analysis.typeCheckingMode": "strict"
}

View file

@ -1,5 +1,6 @@
from time import \ from time import (
sleep # the sleep function adds a delay, allowing time to tick down by a second rather than instantly sleep, # the sleep function adds a delay, allowing time to tick down by a second rather than instantly
)
def countdown(n: int) -> None: def countdown(n: int) -> None:
@ -25,5 +26,5 @@ def count(n: int) -> None: #this part of the code checks to see if a numb
countup(n) #If negative, we'll utilize the countup function countup(n) #If negative, we'll utilize the countup function
if __name__ == "__main__": if __name__ == "__main__":
num = input("Enter a number: ") num: str = input("Enter a number: ")
count(int(num)) count(int(num))

View file

@ -5,29 +5,26 @@ GREEN = "\033[32m" # Green
RESET = "\033[0m" # Reset to default color RESET = "\033[0m" # Reset to default color
def divide(x: float, y: float) -> float | None: def divide(x: float, y: float) -> float:
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
x = float(x) # Tries to convert the x argument into a float y = float(y) # tries to convert the y 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 if y == 0: # This argument checks to see if the inputted y is a 0
return print( raise ZeroDivisionError # If the Y does check out to be a zero, we raise an error that you cannot divide by zero
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) return float(x / y)
if __name__ == "__main__": if __name__ == "__main__":
x = input( x: str = input(
f"{BLUE}Input a number to divide:{RESET} " f"{BLUE}Input a number to divide:{RESET} "
) # Allows us to input a number for X to divide ) # Allows us to input a number for X to divide
y = input( y: str = input(
f"{BLUE}Input a number to divide by:{RESET} " f"{BLUE}Input a number to divide by:{RESET} "
) # Allows us to input a number for Y to divide x by ) # Allows us to input a number for Y to divide x by
result = divide(x, y) try:
if result is not None: result: float = divide(float(x), float(y))
print(f"{GREEN}{result}{RESET}") 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}")

View file

@ -12,10 +12,10 @@ def calculate_hypotenuse(x: float, y: float) -> float:
return sqrt(x**2 + y**2) return sqrt(x**2 + y**2)
if __name__ == "__main__": if __name__ == "__main__":
x = input(f"{BLUE}Input your A:{RESET} ") x: str = input(f"{BLUE}Input your A:{RESET} ")
y = input(f"{BLUE}Input your B:{RESET} ") y: str = input(f"{BLUE}Input your B:{RESET} ")
try: try:
result = calculate_hypotenuse(x,y) result: float = calculate_hypotenuse(float(x), float(y))
print(f"{GREEN}{round(result)} ({result}){RESET}") print(f"{GREEN}{round(result)} ({result}){RESET}")
except ValueError: except ValueError:
print(f"{RED}Please do not input strings.{RESET}") print(f"{RED}Please do not input strings.{RESET}")

View file

@ -1,4 +1,6 @@
prices_dict = { from typing import Any, Literal, Union
prices_dict: dict[str, int] = {
"grapes": 200, "grapes": 200,
"oranges": 400, "oranges": 400,
"apples": 600 "apples": 600
@ -25,11 +27,11 @@ def get_discounted_prices(*prices: int) -> float:
return float(price_sum) return float(price_sum)
def print_row(item: str, price: str, bold: bool = False) -> None: def print_row(item: str, price: Union[str, Any], bold: bool = False) -> None:
# ANSI color codes # ANSI color codes
blue = "\033[34m" # Blue blue = "\033[34m" # Blue
green = "\033[32m" # Green green = "\033[32m" # Green
bold_s = "\033[1m" if bold is True else "" # Bold, use an empty string instead of None, because None will be converted to a string by print() bold_s: Literal['\u001b[1m'] | Literal[''] = "\033[1m" if bold is True else "" # Bold, use an empty string instead of None, because None will be converted to a string by print()
reset = "\033[0m" # Reset to default color reset = "\033[0m" # Reset to default color
# apply ansi styling for bold, then blue, then offset the first string by 25 characters to the left. # apply ansi styling for bold, then blue, then offset the first string by 25 characters to the left.