From e49dbce21071eed21bf978ed141946dda4581bfb Mon Sep 17 00:00:00 2001 From: Pavel Vaks <129676672+PavelLinearB@users.noreply.github.com> Date: Wed, 19 Feb 2025 20:13:11 +0200 Subject: [PATCH 01/13] Create calc.py --- calc/calc.py | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 calc/calc.py diff --git a/calc/calc.py b/calc/calc.py new file mode 100644 index 0000000..efb526c --- /dev/null +++ b/calc/calc.py @@ -0,0 +1,55 @@ +# This function adds two numbers +def add(x, y): + return x + y + +# This function subtracts two numbers +def subtract(x, y): + return x - y + +# This function multiplies two numbers +def multiply(x, y): + return x * y + +# This function divides two numbers +def divide(x, y): + return x / y + + +print("Select operation.") +print("1.Add") +print("2.Subtract") +print("3.Multiply") +print("4.Divide") + +while True: + # take input from the user + choice = input("Enter choice(1/2/3/4): ") + + # check if choice is one of the four options + if choice in ('1', '2', '3', '4'): + try: + num1 = float(input("Enter first number: ")) + num2 = float(input("Enter second number: ")) + except ValueError: + print("Invalid input. Please enter a number.") + continue + + if choice == '1': + print(num1, "+", num2, "=", add(num1, num2)) + + elif choice == '2': + print(num1, "-", num2, "=", subtract(num1, num2)) + + elif choice == '3': + print(num1, "*", num2, "=", multiply(num1, num2)) + + elif choice == '4': + print(num1, "/", num2, "=", divide(num1, num2)) + + # check if user wants another calculation + # break the while loop if answer is no + next_calculation = input("Let's do next calculation? (yes/no): ") + if next_calculation == "no": + break + else: + print("Invalid Input") From 7bbb8292a46ed260817e0fe1489870656622adea Mon Sep 17 00:00:00 2001 From: PavelLinearB Date: Wed, 19 Feb 2025 20:18:51 +0200 Subject: [PATCH 02/13] fixed --- calc/calc.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/calc/calc.py b/calc/calc.py index efb526c..68d9314 100644 --- a/calc/calc.py +++ b/calc/calc.py @@ -12,6 +12,8 @@ def multiply(x, y): # This function divides two numbers def divide(x, y): + if y == 0: + raise ValueError("Cannot divide by zero") return x / y @@ -49,7 +51,7 @@ def divide(x, y): # check if user wants another calculation # break the while loop if answer is no next_calculation = input("Let's do next calculation? (yes/no): ") - if next_calculation == "no": + if next_calculation.lower() == "no": break else: print("Invalid Input") From 24cf4dfe6b021adf73770bd84edda83e990c9823 Mon Sep 17 00:00:00 2001 From: Pavel Vaks <129676672+PavelLinearB@users.noreply.github.com> Date: Wed, 19 Feb 2025 20:27:47 +0200 Subject: [PATCH 03/13] Update calc.py --- calc/calc.py | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/calc/calc.py b/calc/calc.py index 68d9314..4285881 100644 --- a/calc/calc.py +++ b/calc/calc.py @@ -22,7 +22,12 @@ def divide(x, y): print("2.Subtract") print("3.Multiply") print("4.Divide") - +operations = { + '1': (add, '+'), + '2': (subtract, '-'), + '3': (multiply, '*'), + '4': (divide, '/') +} while True: # take input from the user choice = input("Enter choice(1/2/3/4): ") @@ -36,22 +41,13 @@ def divide(x, y): print("Invalid input. Please enter a number.") continue - if choice == '1': - print(num1, "+", num2, "=", add(num1, num2)) - - elif choice == '2': - print(num1, "-", num2, "=", subtract(num1, num2)) - - elif choice == '3': - print(num1, "*", num2, "=", multiply(num1, num2)) - - elif choice == '4': - print(num1, "/", num2, "=", divide(num1, num2)) + func, operator = operations[choice] + print(num1, operator, num2, "=", func(num1, num2)) # check if user wants another calculation # break the while loop if answer is no next_calculation = input("Let's do next calculation? (yes/no): ") if next_calculation.lower() == "no": - break + break else: print("Invalid Input") From 5429335ed47edc0e29e6b51886430fc0a6e494e2 Mon Sep 17 00:00:00 2001 From: Pavel Vaks <129676672+PavelLinearB@users.noreply.github.com> Date: Wed, 19 Feb 2025 20:36:10 +0200 Subject: [PATCH 04/13] Update calc.py --- calc/calc.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/calc/calc.py b/calc/calc.py index 4285881..d77d6fc 100644 --- a/calc/calc.py +++ b/calc/calc.py @@ -47,7 +47,10 @@ def divide(x, y): # check if user wants another calculation # break the while loop if answer is no next_calculation = input("Let's do next calculation? (yes/no): ") + while (next_calculation.lower() != "yes" or next_calculation.lower() != "no"): + next_calculation = input("Let's do next calculation? (yes/no): ") + if next_calculation.lower() == "no": break else: - print("Invalid Input") + print("Invalid Input Please select 1, 2, 3, or 4.") From 2dd3a794d3efd1749f3e597e9308732bce0a9e81 Mon Sep 17 00:00:00 2001 From: Pavel Vaks <129676672+PavelLinearB@users.noreply.github.com> Date: Wed, 19 Feb 2025 20:40:10 +0200 Subject: [PATCH 05/13] Update calc.py --- calc/calc.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/calc/calc.py b/calc/calc.py index d77d6fc..1500629 100644 --- a/calc/calc.py +++ b/calc/calc.py @@ -22,7 +22,7 @@ def divide(x, y): print("2.Subtract") print("3.Multiply") print("4.Divide") -operations = { +OPERATIONS = { '1': (add, '+'), '2': (subtract, '-'), '3': (multiply, '*'), @@ -41,13 +41,13 @@ def divide(x, y): print("Invalid input. Please enter a number.") continue - func, operator = operations[choice] + func, operator = OPERATIONS[choice] print(num1, operator, num2, "=", func(num1, num2)) # check if user wants another calculation # break the while loop if answer is no next_calculation = input("Let's do next calculation? (yes/no): ") - while (next_calculation.lower() != "yes" or next_calculation.lower() != "no"): + while (next_calculation.lower() not in ["yes", "no"]): next_calculation = input("Let's do next calculation? (yes/no): ") if next_calculation.lower() == "no": From ddc621feceebd7e76041535f1705018197e9b792 Mon Sep 17 00:00:00 2001 From: Pavel Vaks <129676672+PavelLinearB@users.noreply.github.com> Date: Wed, 19 Feb 2025 20:43:46 +0200 Subject: [PATCH 06/13] Update calc.py --- calc/calc.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/calc/calc.py b/calc/calc.py index 1500629..7fef920 100644 --- a/calc/calc.py +++ b/calc/calc.py @@ -16,18 +16,19 @@ def divide(x, y): raise ValueError("Cannot divide by zero") return x / y - -print("Select operation.") -print("1.Add") -print("2.Subtract") -print("3.Multiply") -print("4.Divide") OPERATIONS = { '1': (add, '+'), '2': (subtract, '-'), '3': (multiply, '*'), '4': (divide, '/') } + +print("Select operation.") +print("1.Add") +print("2.Subtract") +print("3.Multiply") +print("4.Divide") + while True: # take input from the user choice = input("Enter choice(1/2/3/4): ") @@ -47,8 +48,10 @@ def divide(x, y): # check if user wants another calculation # break the while loop if answer is no next_calculation = input("Let's do next calculation? (yes/no): ") - while (next_calculation.lower() not in ["yes", "no"]): + while True: next_calculation = input("Let's do next calculation? (yes/no): ") + if next_calculation.lower() in ["yes", "no"]: + break if next_calculation.lower() == "no": break From a22085ed130d2e643acdf6601cb63662ebfc0a7d Mon Sep 17 00:00:00 2001 From: Pavel Vaks <129676672+PavelLinearB@users.noreply.github.com> Date: Wed, 19 Feb 2025 20:47:57 +0200 Subject: [PATCH 07/13] Update calc.py --- calc/calc.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/calc/calc.py b/calc/calc.py index 7fef920..2ba5ccd 100644 --- a/calc/calc.py +++ b/calc/calc.py @@ -47,7 +47,6 @@ def divide(x, y): # check if user wants another calculation # break the while loop if answer is no - next_calculation = input("Let's do next calculation? (yes/no): ") while True: next_calculation = input("Let's do next calculation? (yes/no): ") if next_calculation.lower() in ["yes", "no"]: @@ -56,4 +55,4 @@ def divide(x, y): if next_calculation.lower() == "no": break else: - print("Invalid Input Please select 1, 2, 3, or 4.") + print("Invalid Input. Please select 1, 2, 3, or 4.") From 203167411b268693f3bd9bb870f7c61decfd7a9a Mon Sep 17 00:00:00 2001 From: Pavel Vaks <129676672+PavelLinearB@users.noreply.github.com> Date: Wed, 19 Feb 2025 20:55:55 +0200 Subject: [PATCH 08/13] Update calc.py --- calc/calc.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/calc/calc.py b/calc/calc.py index 2ba5ccd..8784b0d 100644 --- a/calc/calc.py +++ b/calc/calc.py @@ -20,7 +20,7 @@ def divide(x, y): '1': (add, '+'), '2': (subtract, '-'), '3': (multiply, '*'), - '4': (divide, '/') + '4': (divide, '/'), } print("Select operation.") @@ -47,12 +47,11 @@ def divide(x, y): # check if user wants another calculation # break the while loop if answer is no - while True: - next_calculation = input("Let's do next calculation? (yes/no): ") - if next_calculation.lower() in ["yes", "no"]: - break - - if next_calculation.lower() == "no": + next_calculation = input("Let's do next calculation? (yes/no): ").lower() + while next_calculation not in ["yes", "no"]: + next_calculation = input("Let's do next calculation? (yes/no): ").lower() + if next_calculation == "no": break + else: print("Invalid Input. Please select 1, 2, 3, or 4.") From 8f07340b87a6d8688bfe19361daca9c229e473e0 Mon Sep 17 00:00:00 2001 From: Pavel Vaks <129676672+PavelLinearB@users.noreply.github.com> Date: Wed, 19 Feb 2025 23:47:38 +0200 Subject: [PATCH 09/13] Update calc.py --- calc/calc.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/calc/calc.py b/calc/calc.py index 8784b0d..9ddb90f 100644 --- a/calc/calc.py +++ b/calc/calc.py @@ -23,11 +23,11 @@ def divide(x, y): '4': (divide, '/'), } -print("Select operation.") -print("1.Add") -print("2.Subtract") -print("3.Multiply") -print("4.Divide") +print("""Select operation. +1.Add +2.Subtract +3.Multiply +4.Divide""") while True: # take input from the user @@ -47,9 +47,10 @@ def divide(x, y): # check if user wants another calculation # break the while loop if answer is no - next_calculation = input("Let's do next calculation? (yes/no): ").lower() - while next_calculation not in ["yes", "no"]: + while True: next_calculation = input("Let's do next calculation? (yes/no): ").lower() + if next_calculation in ["yes", "no"]: + break if next_calculation == "no": break From 7dc5180030dbd67419f5fc107d0c777fc14b050d Mon Sep 17 00:00:00 2001 From: Pavel Vaks <129676672+PavelLinearB@users.noreply.github.com> Date: Thu, 20 Feb 2025 12:11:42 +0200 Subject: [PATCH 10/13] Update calc.py --- calc/calc.py | 68 +++++++++++++++++++++++++++------------------------- 1 file changed, 36 insertions(+), 32 deletions(-) diff --git a/calc/calc.py b/calc/calc.py index 9ddb90f..b6a8809 100644 --- a/calc/calc.py +++ b/calc/calc.py @@ -23,36 +23,40 @@ def divide(x, y): '4': (divide, '/'), } -print("""Select operation. -1.Add -2.Subtract -3.Multiply -4.Divide""") - -while True: - # take input from the user - choice = input("Enter choice(1/2/3/4): ") - - # check if choice is one of the four options - if choice in ('1', '2', '3', '4'): - try: - num1 = float(input("Enter first number: ")) - num2 = float(input("Enter second number: ")) - except ValueError: - print("Invalid input. Please enter a number.") - continue - - func, operator = OPERATIONS[choice] - print(num1, operator, num2, "=", func(num1, num2)) - - # check if user wants another calculation - # break the while loop if answer is no - while True: - next_calculation = input("Let's do next calculation? (yes/no): ").lower() - if next_calculation in ["yes", "no"]: - break - if next_calculation == "no": - break +def main(): + print("""Select operation. + 1.Add + 2.Subtract + 3.Multiply + 4.Divide""") + + while True: + # take input from the user + choice = input("Enter choice(1/2/3/4): ") + + # check if choice is one of the four options + if choice in ('1', '2', '3', '4'): + try: + num1 = float(input("Enter first number: ")) + num2 = float(input("Enter second number: ")) + except ValueError: + print("Invalid input. Please enter a number.") + continue + + func, operator = OPERATIONS[choice] + print(num1, operator, num2, "=", func(num1, num2)) - else: - print("Invalid Input. Please select 1, 2, 3, or 4.") + # check if user wants another calculation + # break the while loop if answer is no + while True: + next_calculation = input("Let's do next calculation? (yes/no): ").lower() + if next_calculation in ["yes", "no"]: + break + if next_calculation == "no": + break + + else: + print("Invalid Input. Please select 1, 2, 3, or 4.") + +if __name__ == "__main__": + main() From b0461a0b4dc354e7ad25a356348adc1df30c5d90 Mon Sep 17 00:00:00 2001 From: Pavel Vaks <129676672+PavelLinearB@users.noreply.github.com> Date: Thu, 20 Feb 2025 13:59:18 +0200 Subject: [PATCH 11/13] Update calc.py --- calc/calc.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/calc/calc.py b/calc/calc.py index b6a8809..23efa55 100644 --- a/calc/calc.py +++ b/calc/calc.py @@ -44,8 +44,12 @@ def main(): continue func, operator = OPERATIONS[choice] - print(num1, operator, num2, "=", func(num1, num2)) - + try: + print(num1, operator, num2, "=", func(num1, num2)) + except ValueError as e: + print(f"Error: {e}") + continue + # check if user wants another calculation # break the while loop if answer is no while True: From e666b545868585ee085f755163200d610f422e6e Mon Sep 17 00:00:00 2001 From: Pavel Vaks <129676672+PavelLinearB@users.noreply.github.com> Date: Thu, 20 Feb 2025 19:31:38 +0200 Subject: [PATCH 12/13] Update calc.py --- calc/calc.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/calc/calc.py b/calc/calc.py index 23efa55..9a57590 100644 --- a/calc/calc.py +++ b/calc/calc.py @@ -34,7 +34,7 @@ def main(): # take input from the user choice = input("Enter choice(1/2/3/4): ") - # check if choice is one of the four options + # check if the choice is one of the four options if choice in ('1', '2', '3', '4'): try: num1 = float(input("Enter first number: ")) From e2963d4bfd171e5631e47a9b9a2ae7cab2fea91d Mon Sep 17 00:00:00 2001 From: Pavel Vaks <129676672+PavelLinearB@users.noreply.github.com> Date: Thu, 20 Feb 2025 19:46:37 +0200 Subject: [PATCH 13/13] Update calc.py --- calc/calc.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/calc/calc.py b/calc/calc.py index 9a57590..42a98a6 100644 --- a/calc/calc.py +++ b/calc/calc.py @@ -50,7 +50,7 @@ def main(): print(f"Error: {e}") continue - # check if user wants another calculation + # check if the user wants another calculation # break the while loop if answer is no while True: next_calculation = input("Let's do next calculation? (yes/no): ").lower()