diff --git a/challenges/01-calc.py b/challenges/01-calc.py index 87f5190..2a2cfa1 100644 --- a/challenges/01-calc.py +++ b/challenges/01-calc.py @@ -2,3 +2,65 @@ # input() always returns a string value. If you ever want someone # to enter a number you have to use the `int()` function to convert # what they typed in to a string. + +is_running = True + +while is_running: + # display math choices and ask user to chose one + prompt = ">" + print("Hello, User🙋‍♂️") + print("Tell me what kind of math you would like to do") + print("possible choices: add, sub, mul, div") + math_choice = input(prompt) + print(f"math choice: {math_choice}") + + + # ask the user to input two numbers + print ("Excellent math choice! Enter the first number to use") + number_one = input(prompt) + print("Now, enter a second Number") + number_two = input(prompt) + + try: + # convert input to integers + number_one = int(number_one) + number_two = int(number_two) + + # if/elif/else + # og dictionary switch + switch = { + "add": number_one + number_two, + "sub": number_one - number_two, + "mul": number_one * number_two, + "div": number_one / number_two + } + + if math_choice in switch: + print(f"result: {switch[math_choice]}") + else: + # defualt for the switch case + print(f"I lied, {math_choice} isn't a math I know about") + + # match case (new python switch) + match math_choice: + case "add": + print(f"{number_one} + {number_two} = {number_one + number_two}") + case "sub": + print(f"{number_one} - {number_two} = {number_one - number_two}") + case "mul": + print(f"{number_one} * {number_two} = {number_one * number_two}") + case "div": + print(f"{number_one} / {number_two} = {number_one / number_two}") + case _: + print(f"{number_one} makes no sense to me! ") + + except ValueError as e: + # handle error instead of crashing + print("That wasn't a real number!") + + + # ask the user if they would like to exit + print("would you like to contine doin math with me [y/n]?") + should_quit = input(prompt) + if should_quit == "n": + is_running = False \ No newline at end of file diff --git a/challenges/02-reverse.py b/challenges/02-reverse.py index 8cf2677..f40aba2 100644 --- a/challenges/02-reverse.py +++ b/challenges/02-reverse.py @@ -6,3 +6,15 @@ # several ways to reverse a string, and it's a good read! # # http://www.techbeamers.com/essential-python-tips-tricks-programmers/?utm_source=mybridge&utm_medium=blog&utm_campaign=read_more#tip1 + +string = "Hello" + +reversed_string = reversed(string) +new_string = "" +for letter in reversed(string): + new_string =+ letter + +print(new_string) + + +# print("".join(reversed(string))) \ No newline at end of file diff --git a/challenges/03-bank.py b/challenges/03-bank.py index 554cb1d..d147124 100644 --- a/challenges/03-bank.py +++ b/challenges/03-bank.py @@ -1,3 +1,49 @@ print("Welcome to Chase bank.") print("Have a nice day!") +class BankAccount: + def __init__(self): + self.balance = 0 + + def deposit(self, amount): + self.balance += amount + return self.balance + + def withdraw(self, amount): + if amount > self.balance: + print("Insufficient balance!") + return self.balance + else: + self.balance -= amount + return self.balance + + def check_balance(self): + return self.balance + + +def main(): + account = BankAccount() + + while True: + print(f"Your current balance is\n{account.check_balance()}") + action = input("What would you like to do? (deposit, withdraw, check_balance)\n") + + if action.lower() == "deposit": + amount = float(input("How much would you like to deposit?\n")) + account.deposit(amount) + elif action.lower() == "withdraw": + amount = float(input("How much would you like to withdraw?\n")) + account.withdraw(amount) + elif action.lower() == "check_balance": + pass + else: + print("Invalid action. Please enter 'deposit', 'withdraw' or 'check_balance'.") + + done = input("Are you done? (yes/no)\n") + if done.lower() == 'yes': + print("Thank you!") + break + + +if __name__ == "__main__": + main() diff --git a/challenges/04-alphabetical.py b/challenges/04-alphabetical.py index 5051ec4..23a8bdb 100644 --- a/challenges/04-alphabetical.py +++ b/challenges/04-alphabetical.py @@ -1,3 +1,22 @@ # You'll need to use a couple of built in functions to alphabetize a string. # Try to avoid looking up the exact answer and look at built in functions for # lists and strings instead. + + +# convert to lsit approch +string = "sjndgnsndflafdkjsngnsdffgh" +string_list = list(string) +string_list.sort() +string = "".join(string_list) +print(string) + +# sorted function approach +print("".join(sorted(string))) + + +# python slice syntax +my_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] +# [slice start (default 0): slice end(deflaut 0): step (defualt 1)] +my_list_copy = my_list[::] # make a copy with defaults +# print(my_list[::2]) # increments by 2's +# print(my_list[::-1]) # reverses the list