diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..d99f2f3 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,6 @@ +{ + "[python]": { + "editor.defaultFormatter": "ms-python.black-formatter" + }, + "python.formatting.provider": "none" +} \ No newline at end of file diff --git a/challenges/01-calc.py b/challenges/01-calc.py index 87f5190..158cf83 100644 --- a/challenges/01-calc.py +++ b/challenges/01-calc.py @@ -2,3 +2,80 @@ # 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: +# operation = input("Which calculation would you like to do? (add, sub, mult, div) \n") +# num1 = float(input("what is number 1? \n")) +# num2 = float(input("what is number 2? \n")) + +# if operation == 'add': +# result = num1 + num2 +# elif operation == 'sub': +# result = num1 - num2 +# elif operation == 'mult': +# result = num1 * num2 +# elif operation == 'div': +# if num2 != 0: +# result = num1 / num2 +# else: +# print("IMPOSSIBLE to divide by ZERO") +# else: +# print("That wasn't an option... (add, sub, mult, div)") +# print(f"Your result is {result}.") + +# done = input("Do another calculation? (yes, no)\n") +# if done.lower() == "no": +# break + +is_running = True + +while is_running: + # display math choices and ask user to choose one + prompt = "> " + print("Hello, user") + print("What kind of math would you like to do?") + print("add, sub, mul, div?") + math_choice = input(prompt) + print (f"math choice: {math_choice}") + + #ask the user to input two numbers, one by one + print("Enter the first number to use.") + number_one = input(prompt) + print("Now the second number.") + number_two = input(prompt) + + print(number_one, number_two) + + try: + # convert input to integers + number_one = int(number_one) + number_two = int(number_two) + + # 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: + #default for the switch case + print(f"I lied, {math_choice} isn't a math I know about!") + + except ValueError as e: + print("That wasn't a real number!") + + # ask the user if they would like to exit + print("Would you like to continue doing math? (y/n)\n") + should_quit = input(prompt) + if should_quit == "n": + is_running = False + + + + diff --git a/challenges/02-reverse.py b/challenges/02-reverse.py index 8cf2677..bc56f1a 100644 --- a/challenges/02-reverse.py +++ b/challenges/02-reverse.py @@ -6,3 +6,13 @@ # 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 + +def reverse_str(str): + reversed_str = "" + i = len(str) - 1 + while i >= 0: + reversed_str += str[i] + i -= 1 + return reversed_str + +print(reverse_str("abcdef")) \ No newline at end of file diff --git a/challenges/03-bank.py b/challenges/03-bank.py index 554cb1d..4277f29 100644 --- a/challenges/03-bank.py +++ b/challenges/03-bank.py @@ -1,3 +1,62 @@ -print("Welcome to Chase bank.") -print("Have a nice day!") +balance = 4000 +def banking(): + print("Welcome to Chase bank.") + + global balance + + transaction = input("What would you like to do? (deposit, withdraw, balance)\n") + + if transaction == 'deposit': + deposit_amount = float(input("How much would you like to deposit?\n")) + balance += deposit_amount + return f"Your new balance is {balance}." + elif transaction == 'withdraw': + withdraw_amount = float(input("How much would you like to withdraw?\n")) + balance -= withdraw_amount + return f"Your new balance is {balance}." + elif transaction == 'balance': + return f"Your current balance is {balance}." + + print("Have a nice day!") + +# print(banking()) + +def deposit(amount): + global balance + balance += amount + return balance + +def withdraw(amount): + global balance + if amount <= balance: #check if sufficient balance exists + balance -= amount + else: + print("Insufficient balance.") + return balance + +def check_balance(): + global balance + return balance + +while True: + print(f"Your current balance is {balance}.") + transaction = input("What would you like to do? (deposit, withdraw, check_balance)\n") + + if transaction == "deposit": + amount = int(input("How much would you like to deposit?\n")) + balance = deposit(amount) + print(f"Your current balance is {balance}") + elif transaction == "withdraw": + amount = int(input("How much would you like to withdraw?\n")) + balance = withdraw(amount) + print(f"Your current balance is {balance}") + elif transaction == "check_balance": + print(f"Your current balance is {balance}") + else: + print("Not a valid input. Select deposit, withdraw, check_balance.\n") + + done = input("Are you done? (yes, no)\n") + if done.lower() == "yes": + print("Thank you!") + break \ No newline at end of file diff --git a/challenges/04-alphabetical.py b/challenges/04-alphabetical.py index 5051ec4..3602758 100644 --- a/challenges/04-alphabetical.py +++ b/challenges/04-alphabetical.py @@ -1,3 +1,28 @@ # 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. + +def alphabetize(str): + # sorted_str = sorted(str) + + # stringified = ''.join(sorted_str) + + # return stringified + + return ''.join(sorted(str)) + +print(alphabetize("supercalifragilisticexpialidocious")) + +# convert to list approach +string = "antidisestablishmentarianism" +string_list = list(string) +string_list.sort() +string = "".join(string_list) +print(string) + +# python slice syntax +my_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] +# [slice start (default 0): slice end (default end of list): step (default 1)] +my_list_copy = my_list[::] # make a copy with defaults +print(my_list[::-1]) +print("racecar"[::-1]) \ No newline at end of file