Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add new problem#25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
samail-islam wants to merge5 commits intoProgrammingHero1:master
base:master
Choose a base branch
Loading
fromsamail-islam:master
Open
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletionsHarder/Advanced-Calculator.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -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`
3 changes: 2 additions & 1 deletionREADME.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -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
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp