Compare commits
3 commits
39b645e0eb
...
3976c7d8bc
Author | SHA1 | Date | |
---|---|---|---|
3976c7d8bc | |||
d9087c0a7d | |||
232cc60d19 |
3 changed files with 46 additions and 26 deletions
7
Idk tbh/I have no clue
Normal file
7
Idk tbh/I have no clue
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
Nested conditional
|
||||||
|
if x > 10:
|
||||||
|
if y > 10:
|
||||||
|
result = "Both greater"
|
||||||
|
else: result = "x is greater"
|
||||||
|
else: result = "x is not greater" # Equivalent single conditional if x > 10 and y > 10: result = "Both greater" elif x > 10: result = "x is greater" else: result = "x is not greater"
|
||||||
|
function
|
|
@ -1,27 +1,32 @@
|
||||||
from time import sleep
|
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:
|
||||||
if n <= 0:
|
if n <= 0: #If a number is less than or equal to 0,
|
||||||
print('Blastoff!')
|
print('Blastoff!') #Blastoff! is printed, else
|
||||||
else:
|
else:
|
||||||
print(n)
|
print(n) #we print the current number inputted,
|
||||||
sleep(1)
|
sleep(1) #The code is delayed by a second to replicate an actual countdown
|
||||||
countdown(n-1)
|
countdown(n-1) #The code deducts 1 from our inputted number, until it reaches 0.
|
||||||
|
|
||||||
def countup(n: int) -> None:
|
def countup(n: int) -> None:
|
||||||
if n >= 0:
|
if n >= 0: #we're definining the countup function, if a number is 0 or greater
|
||||||
print('Blastoff!')
|
print('Blastoff!') #if the above condition is met, Blastoff! is printed, else
|
||||||
else:
|
else:
|
||||||
print(n)
|
print(n) #we print the number
|
||||||
sleep(1)
|
sleep(1) #delay the code by a second to match an actual countdown(countup in this instance)
|
||||||
countup(n+1)
|
countup(n+1) #We add 1 to our number, since with a countup, we'll be dealing with negatives going towards 0
|
||||||
|
|
||||||
def count(n: int) -> None:
|
def count(n: int) -> None: #this part of the code checks to see if a number is positive or negative
|
||||||
if int(n) >= 0:
|
if int(n) >= 0: #We're checking for if a positive number is inputted, or negative
|
||||||
countdown(n)
|
countdown(n) #If positive, we'll utilize the Countdown function
|
||||||
else:
|
else:
|
||||||
countup(n)
|
countup(n) #If negative, we'll utilize the countup function
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
num = input("Enter a number: ")
|
num = input("Enter a number: ")
|
||||||
count(int(num))
|
count(int(num))
|
||||||
|
|
|
@ -6,20 +6,28 @@ RESET = "\033[0m" # Reset to default color
|
||||||
|
|
||||||
|
|
||||||
def divide(x: float, y: float) -> float | None:
|
def divide(x: float, y: float) -> float | None:
|
||||||
try:
|
try: # This function checks to see if we can convert the input into a float
|
||||||
x = float(x)
|
x = float(x) # Tries to convert the x argument into a float
|
||||||
y = float(y)
|
y = float(y) # tries to convert the y argument into a float
|
||||||
except ValueError:
|
except ValueError:
|
||||||
return print(f"{RED}You cannot divide by strings!{RESET}")
|
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:
|
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}") # you don't divide by zero
|
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)
|
return float(x / y)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
x = input(f"{BLUE}Input a number to divide:{RESET} ")
|
x = input(
|
||||||
y = input(f"{BLUE}Input a number to divide by:{RESET} ")
|
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)
|
result = divide(x, y)
|
||||||
if result is not None:
|
if result is not None:
|
||||||
print(f"{GREEN}{result}{RESET}")
|
print(f"{GREEN}{result}{RESET}")
|
||||||
|
|
Loading…
Reference in a new issue