From 0dab46d30c05eb5702998858e918f835735a554b Mon Sep 17 00:00:00 2001 From: Ilyas Shaikh <72152969+ilyas829@users.noreply.github.com> Date: Thu, 3 Oct 2024 16:02:21 +0000 Subject: [PATCH 1/3] Added Calculator code in python --- Add Code Here/calculator.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 Add Code Here/calculator.py diff --git a/Add Code Here/calculator.py b/Add Code Here/calculator.py new file mode 100644 index 00000000000..9167ceeafe9 --- /dev/null +++ b/Add Code Here/calculator.py @@ -0,0 +1,32 @@ +def add(a,b): + return (a+b) +def sub(a,b): + return (a-b) +def mult(a,b): + return (a*b) +def div(a,b): + return (a//b) + + +print("1. Addition") +print("2 . Subtraction") +print("3. Multiplication") +print("4. Division") + + +while True: + choice = int(input("Enter Choice ")) + if choice == 1 or choice ==2 or choice ==3 or choice ==4 or choice ==5: + a= int(input("Enter 1st number")) + b= int(input("Enter 2nd number")) + if choice == 1: + print(add(a,b)) + elif choice == 2: + print(sub(a,b)) + elif choice == 3: + print(mult(a,b)) + elif choice == 4: + print(div(a,b)) + else: + print("Enter valid choice") + break \ No newline at end of file From ccfce883fec274111ff61ff3fb5c11637096fa22 Mon Sep 17 00:00:00 2001 From: Ilyas Shaikh <72152969+ilyas829@users.noreply.github.com> Date: Thu, 3 Oct 2024 16:08:15 +0000 Subject: [PATCH 2/3] I was here @ hactoberfest --- content/ilyas829.md | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 content/ilyas829.md diff --git a/content/ilyas829.md b/content/ilyas829.md new file mode 100644 index 00000000000..c093726bf98 --- /dev/null +++ b/content/ilyas829.md @@ -0,0 +1,4 @@ +name: ilyas829 +institution: SIBAR +github:[**ilyas829**](https://github.com/ilyas829) +Ilyas was here \ No newline at end of file From 11111620f104db2dea1c3277b511441063a64976 Mon Sep 17 00:00:00 2001 From: Ilyas Shaikh <72152969+ilyas829@users.noreply.github.com> Date: Thu, 3 Oct 2024 16:22:20 +0000 Subject: [PATCH 3/3] Converted my code in class and oject --- Add Code Here/calculator.py | 84 +++++++++++++++++++++++-------------- 1 file changed, 53 insertions(+), 31 deletions(-) diff --git a/Add Code Here/calculator.py b/Add Code Here/calculator.py index 9167ceeafe9..e9c86a66c11 100644 --- a/Add Code Here/calculator.py +++ b/Add Code Here/calculator.py @@ -1,32 +1,54 @@ -def add(a,b): - return (a+b) -def sub(a,b): - return (a-b) -def mult(a,b): - return (a*b) -def div(a,b): - return (a//b) - - -print("1. Addition") -print("2 . Subtraction") -print("3. Multiplication") -print("4. Division") - - -while True: - choice = int(input("Enter Choice ")) - if choice == 1 or choice ==2 or choice ==3 or choice ==4 or choice ==5: - a= int(input("Enter 1st number")) - b= int(input("Enter 2nd number")) - if choice == 1: - print(add(a,b)) - elif choice == 2: - print(sub(a,b)) - elif choice == 3: - print(mult(a,b)) - elif choice == 4: - print(div(a,b)) +class Calculator: + def __init__(self): + pass + + def add(self, a, b): + return a + b + + def sub(self, a, b): + return a - b + + def mult(self, a, b): + return a * b + + def div(self, a, b): + if b != 0: + return a // b else: - print("Enter valid choice") - break \ No newline at end of file + return "Division by zero is not allowed" + +class Calculator: + def __init__(self): + self.calculator = Calculator() + + def display_menu(self): + print("1. Addition") + print("2. Subtraction") + print("3. Multiplication") + print("4. Division") + + def process_choice(self): + while True: + choice = int(input("Enter Choice (1-4): ")) + if choice in [1, 2, 3, 4]: + a = int(input("Enter 1st number: ")) + b = int(input("Enter 2nd number: ")) + + if choice == 1: + print(self.calculator.add(a, b)) + elif choice == 2: + print(self.calculator.sub(a, b)) + elif choice == 3: + print(self.calculator.mult(a, b)) + elif choice == 4: + print(self.calculator.div(a, b)) + else: + print("Invalid choice. Exiting...") + break + + +# Create an instance of the ZeroDay class and run the program +if __name__ == "__main__": + Calculator = Calculator() + Calculator.display_menu() + Calculator.process_choice()