diff --git a/Harder/Advanced-Calculator.md b/Harder/Advanced-Calculator.md new file mode 100644 index 0000000..02638c3 --- /dev/null +++ b/Harder/Advanced-Calculator.md @@ -0,0 +1,87 @@ +# 12.6 Advanced Calculator + +## The task +Create a calculator. That will be able to take user input of an math operation and perform it. + +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 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 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 +```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 + +# 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 = input("Enter your calculation: ") +calclist = calc.split() +num1 = int(calclist[0]) +operation = calclist[1] +num2 = int(calclist[2]) + + +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 input. Then using `split()` we are splitting that and getting two numbers and the operator. + +Then we have if-elif-else, 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` 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