From 8c42ca10001c8bf18627f4be82b3df6b176fc2b0 Mon Sep 17 00:00:00 2001 From: Heiner000 Date: Thu, 11 May 2023 20:48:35 -0700 Subject: [PATCH 1/6] finishes calc --- challenges/01-calc.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/challenges/01-calc.py b/challenges/01-calc.py index 87f5190..97a3f6f 100644 --- a/challenges/01-calc.py +++ b/challenges/01-calc.py @@ -2,3 +2,25 @@ # 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. + +def calculator(): + operation = input("Which calculation would you like to do? (add, sub, mult, div) ") + num1 = int(input("what is number 1? ")) + num2 = int(input("what is number 2? ")) + + 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: + return "IMPOSSIBLE to divide by ZERO" + else: + return "That wasn't an option... (add, sub, mult, div)" + return f"Your result is {result}." + +print(calculator()) \ No newline at end of file From e6ce597f71ad8ff5b0d98902d02579d20f33ac5d Mon Sep 17 00:00:00 2001 From: Heiner000 Date: Thu, 11 May 2023 21:58:07 -0700 Subject: [PATCH 2/6] 03-bank working --- challenges/01-calc.py | 4 ++-- challenges/02-reverse.py | 10 ++++++++++ challenges/03-bank.py | 24 ++++++++++++++++++++++-- 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/challenges/01-calc.py b/challenges/01-calc.py index 97a3f6f..fe58c12 100644 --- a/challenges/01-calc.py +++ b/challenges/01-calc.py @@ -5,8 +5,8 @@ def calculator(): operation = input("Which calculation would you like to do? (add, sub, mult, div) ") - num1 = int(input("what is number 1? ")) - num2 = int(input("what is number 2? ")) + num1 = float(input("what is number 1? ")) + num2 = float(input("what is number 2? ")) if operation == 'add': result = num1 + num2 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..28d2cd3 100644 --- a/challenges/03-bank.py +++ b/challenges/03-bank.py @@ -1,3 +1,23 @@ -print("Welcome to Chase bank.") -print("Have a nice day!") +balance = 100 +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()) From d6391829c8ebcf1ab2fe5b8c9d6d988b09d6bc1e Mon Sep 17 00:00:00 2001 From: Heiner000 Date: Thu, 11 May 2023 22:09:48 -0700 Subject: [PATCH 3/6] adds another way to do 03-bank --- challenges/03-bank.py | 43 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/challenges/03-bank.py b/challenges/03-bank.py index 28d2cd3..4277f29 100644 --- a/challenges/03-bank.py +++ b/challenges/03-bank.py @@ -1,4 +1,4 @@ -balance = 100 +balance = 4000 def banking(): print("Welcome to Chase bank.") @@ -20,4 +20,43 @@ def banking(): print("Have a nice day!") -print(banking()) +# 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 From e10999a8e9e51b7709c8eb03b914a8c8998c5973 Mon Sep 17 00:00:00 2001 From: Heiner000 Date: Thu, 11 May 2023 22:29:34 -0700 Subject: [PATCH 4/6] finishes 04-alphabetize --- challenges/04-alphabetical.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/challenges/04-alphabetical.py b/challenges/04-alphabetical.py index 5051ec4..eabcb03 100644 --- a/challenges/04-alphabetical.py +++ b/challenges/04-alphabetical.py @@ -1,3 +1,12 @@ # 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 + +print(alphabetize("supercalifragilisticexpialidocious")) \ No newline at end of file From 9e2802b04a21be7ec721baf090392f074f802f1f Mon Sep 17 00:00:00 2001 From: Heiner000 Date: Thu, 11 May 2023 22:32:39 -0700 Subject: [PATCH 5/6] simplifies 04-alphabetize --- challenges/04-alphabetical.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/challenges/04-alphabetical.py b/challenges/04-alphabetical.py index eabcb03..eeeee65 100644 --- a/challenges/04-alphabetical.py +++ b/challenges/04-alphabetical.py @@ -3,10 +3,12 @@ # lists and strings instead. def alphabetize(str): - sorted_str = sorted(str) + # sorted_str = sorted(str) - stringified = ''.join(sorted_str) + # stringified = ''.join(sorted_str) - return stringified + # return stringified + + return ''.join(sorted(str)) print(alphabetize("supercalifragilisticexpialidocious")) \ No newline at end of file From cc9aa1f1882626c7b3451767f5afc7f9dbbb50e6 Mon Sep 17 00:00:00 2001 From: Heiner000 Date: Fri, 12 May 2023 10:12:03 -0700 Subject: [PATCH 6/6] finishes in class review --- .vscode/settings.json | 6 +++ challenges/01-calc.py | 93 ++++++++++++++++++++++++++++------- challenges/04-alphabetical.py | 16 +++++- 3 files changed, 95 insertions(+), 20 deletions(-) create mode 100644 .vscode/settings.json 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 fe58c12..158cf83 100644 --- a/challenges/01-calc.py +++ b/challenges/01-calc.py @@ -3,24 +3,79 @@ # to enter a number you have to use the `int()` function to convert # what they typed in to a string. -def calculator(): - operation = input("Which calculation would you like to do? (add, sub, mult, div) ") - num1 = float(input("what is number 1? ")) - num2 = float(input("what is number 2? ")) - - 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 +# 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: - return "IMPOSSIBLE to divide by ZERO" - else: - return "That wasn't an option... (add, sub, mult, div)" - return f"Your result is {result}." + #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 + + + -print(calculator()) \ No newline at end of file diff --git a/challenges/04-alphabetical.py b/challenges/04-alphabetical.py index eeeee65..3602758 100644 --- a/challenges/04-alphabetical.py +++ b/challenges/04-alphabetical.py @@ -11,4 +11,18 @@ def alphabetize(str): return ''.join(sorted(str)) -print(alphabetize("supercalifragilisticexpialidocious")) \ No newline at end of file +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