From 85127c0eed8b96d554c78e2e6ca919214e2e9f34 Mon Sep 17 00:00:00 2001 From: Samail Islam Date: Sat, 19 Apr 2025 22:06:14 +0600 Subject: [PATCH 1/5] Create Unique-Calculator.md --- Harder/Unique-Calculator.md | 76 +++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 Harder/Unique-Calculator.md diff --git a/Harder/Unique-Calculator.md b/Harder/Unique-Calculator.md new file mode 100644 index 0000000..85fb108 --- /dev/null +++ b/Harder/Unique-Calculator.md @@ -0,0 +1,76 @@ +# Under development....... +# 12.6 Unique Calculator + +## The task +Create a simple calculator. That will be able to take user input of two numbers and the operation the user wants to perform. + +But it would be a little different from other python calculator projects those you usually see. +It will take the calculation directly from the user and then perform it, without asking user to choose calculation type( addition, multiplication etc.) + +## Solution strategy +Create a bunch of functions to perform add, subtract, multiply, division or modulo. + +Then take the calculation from the user. + +As the calculator can perform only two number's calculations, you can use the built in split() method + +Then call the appropriate function based on the operation. + +Think for a few minutes and try it yourself first. + +## The solution +```python +def add(num1, num2): + return num1 + num2 + +def subtract(num1, num2): + return num1 - num2 + +def multiply(num1, num2): + return num1 * num2 + +def divide(num1, num2): + return num1 / num2 + +def modulo(num1, num2): + return num1 % num2 + +# Take input from the user +num1 = int(input("Enter first number: ")) +operation = input("What you want to do(+, -, *, /, %):") +num2 = int(input("Enter second number: ")) + +result = 0 +if operation == '+': + result = add(num1,num2) +elif operation == '-': + result = subtract(num1,num2) +elif operation == '*': + result = multiply(num1,num2) +elif operation == '/': + result = divide(num1,num2) +elif operation == '%': + result = modulo(num1,num2) +else: + print("Please enter: +, -, *, / or %") + +print(num1, operation, num2, '=', result) +``` +**[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** + + +## How it works +You saw five functions to add, subtracts, etc. Those are easy. + +Then we are taking user inputs. Three inputs. They are easy too. + +Then we have if-elif-else. And based on the operation, we call the right method to perform the task. + +That’s it. + + +  +[![Next Page](../assets/next-button.png)](Password-generator.md) +  + +tags: `programming-hero` `python` `python3` `problem-solving` `programming` `coding-challenge` `interview` `learn-python` `python-tutorial` `programming-exercises` `programming-challenges` `programming-fundamentals` `programming-contest` `python-coding-challenges` `python-problem-solving` From b120929012d9478b7b33f5fc2d6002059551d433 Mon Sep 17 00:00:00 2001 From: Samail Islam Date: Mon, 21 Apr 2025 18:30:00 +0600 Subject: [PATCH 2/5] Advanced-Calculator.md --- ...e-Calculator.md => Advanced-Calculator.md} | 24 ++++++++++++------- 1 file changed, 15 insertions(+), 9 deletions(-) rename Harder/{Unique-Calculator.md => Advanced-Calculator.md} (69%) diff --git a/Harder/Unique-Calculator.md b/Harder/Advanced-Calculator.md similarity index 69% rename from Harder/Unique-Calculator.md rename to Harder/Advanced-Calculator.md index 85fb108..30382e2 100644 --- a/Harder/Unique-Calculator.md +++ b/Harder/Advanced-Calculator.md @@ -1,20 +1,23 @@ # Under development....... -# 12.6 Unique Calculator +# 12.6 Advanced Calculator ## The task -Create a simple calculator. That will be able to take user input of two numbers and the operation the user wants to perform. +Create a calculator. That will be able to take user input of an math operation and perform it. -But it would be a little different from other python calculator projects those you usually see. -It will take the calculation directly from the user and then perform it, without asking user to choose calculation type( addition, multiplication etc.) +But it would be a little advanced from other python calculator projects those you usually see. + +It will take the calculation directly from the user and then perform it, without asking user to choose operation type( addition, multiplication etc.). ## Solution strategy Create a bunch of functions to perform add, subtract, multiply, division or modulo. Then take the calculation from the user. -As the calculator can perform only two number's calculations, you can use the built in split() method +As the calculator can perform only two number's calculations, you can use the built in ``split()`` method to make the input a list containing three elements. + +The input list's first element would be the first number, second element the operator( +,*,/,-,%) and last one the second number. -Then call the appropriate function based on the operation. +Then put the numbers in separate variables and call the appropriate function based on the operator. Think for a few minutes and try it yourself first. @@ -36,9 +39,12 @@ def modulo(num1, num2): return num1 % num2 # Take input from the user -num1 = int(input("Enter first number: ")) -operation = input("What you want to do(+, -, *, /, %):") -num2 = int(input("Enter second number: ")) +calc = int(input("Enter your calculation: ")) +calclist = calc.split() +num1 = int(calclist[0]) +operation = calclist[1] +num2 = int(calclist[2]) + result = 0 if operation == '+': From 38d8816691c039e160939263e4a3f9202c9ba993 Mon Sep 17 00:00:00 2001 From: Samail Islam Date: Mon, 21 Apr 2025 21:10:01 +0600 Subject: [PATCH 3/5] Update Advanced-Calculator.md --- Harder/Advanced-Calculator.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Harder/Advanced-Calculator.md b/Harder/Advanced-Calculator.md index 30382e2..160e36a 100644 --- a/Harder/Advanced-Calculator.md +++ b/Harder/Advanced-Calculator.md @@ -1,4 +1,3 @@ -# Under development....... # 12.6 Advanced Calculator ## The task @@ -68,7 +67,7 @@ print(num1, operation, num2, '=', result) ## How it works You saw five functions to add, subtracts, etc. Those are easy. -Then we are taking user inputs. Three inputs. They are easy too. +Then we are taking user input. Then using `split()` we are splitting that and getting two numbers and the operator. Then we have if-elif-else. And based on the operation, we call the right method to perform the task. From 477a12ee5bb7cb08f3889b343d777a49543e7c1e Mon Sep 17 00:00:00 2001 From: Samail Islam Date: Mon, 21 Apr 2025 22:17:37 +0600 Subject: [PATCH 4/5] Update Advanced-Calculator.md --- Harder/Advanced-Calculator.md | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/Harder/Advanced-Calculator.md b/Harder/Advanced-Calculator.md index 160e36a..02638c3 100644 --- a/Harder/Advanced-Calculator.md +++ b/Harder/Advanced-Calculator.md @@ -18,6 +18,9 @@ The input list's first element would be the first number, second element the ope Then put the numbers in separate variables and call the appropriate function based on the operator. +> The `split()` method is a built in function in python, which splits a string into a list where the string has a space. +> Exmp: split("2 + 3") will return ["2","+","3"] as result. + Think for a few minutes and try it yourself first. ## The solution @@ -36,9 +39,12 @@ def divide(num1, num2): def modulo(num1, num2): return num1 % num2 - + +# Inform user that each element in the input should be separated by space. +print("Give space among the numbers and operator, like: 24 - 5 ") + # Take input from the user -calc = int(input("Enter your calculation: ")) +calc = input("Enter your calculation: ") calclist = calc.split() num1 = int(calclist[0]) operation = calclist[1] @@ -69,7 +75,7 @@ You saw five functions to add, subtracts, etc. Those are easy. Then we are taking user input. Then using `split()` we are splitting that and getting two numbers and the operator. -Then we have if-elif-else. And based on the operation, we call the right method to perform the task. +Then we have if-elif-else, based on the operation, we call the right method to perform the task. That’s it. From 591ec2433bf0055a4b6bb79b1e5309a9b7dac26e Mon Sep 17 00:00:00 2001 From: Samail Islam Date: Mon, 21 Apr 2025 22:41:28 +0600 Subject: [PATCH 5/5] Update README.md --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index c2a8c6b..50ce1d9 100644 --- a/README.md +++ b/README.md @@ -102,7 +102,8 @@ Here, we will take a real-world coding related problem. We will think about the * **[12.2](Harder/Password-generator.md "Password generator")** -   **[Password generator](Harder/Password-generator.md)** * **[12.3](Harder/Password-with-requirements.md "Password with requirements")** -   **[Password with requirements](Harder/Password-with-requirements.md)** * **[12.4](Harder/Permutations.md "Permutations")** -   **[Permutations](Harder/Permutations.md)** -* **[12.5](Harder/Simple-Calculator.md "Generate Sentences")** -   **[Generate Sentences](Harder/Simple-Calculator.md)** +* **[12.5](Harder/Generate-Sentences.md "Generate Sentences")** -   **[Generate Sentences](Harder/Generate-Sentences.md)** +* **[12.6](Harder/Advanced-Calculator.md "Advanced calculator")** -   **[Advanced calculator](Harder/Advanced-Calculator.md)** ## 13 -  User Submitted