Python: Creating a calculator

joushe info
2022-04-09
0

...

Have you ever asked yourself how a calculator works? Well, in this section, you will learn the concept behind it and create your own basic calculator with the four fundamental mathematical operations with just a few lines of code.

Have you ever asked yourself how a calculator works? Well, in this section, you will learn the concept behind it and create your own basic calculator with the four fundamental mathematical operations with just a few lines of code.

Please note that the numbers to operate can be any. For example, the number 2 is a natural number, while Pi is an irrational number.

As in mathematics, Python also classifies numbers according to their nature. The number 2 is of type integer in Python, while the number pi is of type float.

#The user defines the first and second numbers
num1 = float(input("Type your desired number:"))
num2 = float(input("Type your desired number:"))

Here, the function input allows the user to type the desired number. However, this number is not considered a number by Python. In other words, Python will take the introduced number as a string. For this reason, we should write the function float, which allows Python to understand that the input entered by the user should be a real number.

Once the numbers are defined, how can we make Python understand the type of operation we would like to do? In a similar way to the numbers, the user will also introduce the desired math operation.

#The user defines the desired math operation
operation = input("Type the math basic operation you would like to do:")

Once the user defines the math operation, Python should take this input to understand what to do next. For example, if you entered the numbers 2 and Pi, and you would like to add or multiply them, when you write "addition" or "multiplication", Python should be able to understand the input and make the respective operation.

if operation == "addition":
   print(f"result = {num1 + num2}")
elif operation == "substraction":
   print(f"result = {num1 - num2:.5f}")
elif operation == "multiplication":
   print(f"result = {num1 * num2}")

In case we want to divide 2 numbers, we must keep in mind that division by 0 is not possible. For this reason, we should exclude this operation. And in case the user for any reason enters the number 0 as the denominator, we can tell Python to remind the user that this operation is not possible.

elif num2 != 0 and operation == "division":
   print(f"result = {num1 / num2}")

else: print("Invalid operation")

Congratulations! You just created your own calculator in Python with few lines of code! The final Python code will look like this:

#The user defines the first and second numbers
num1 = float(input("Type your desired number:"))
num2 = float(input("Type your desired number:"))

#The user defines the desired math operation operation = input("Type the math basic operation you would like to do:")
if operation == "addition": print(f"result = {num1 + num2}") elif operation == "substraction": print(f"result = {num1 - num2:.5f}") elif operation == "multiplication": print(f"result = {num1 * num2}") elif num2 != 0 and operation == "division": print(f"result = {num1 / num2}") else: print("Invalid operation")

After running the code, we will get the following output.

Now, let's test if Python is capable of make division by 0.

As we can see, if the user provides the number 0 as the denominator, Python will output "Invalid operation".

Views: 1

joushe info

Looking for new horizons

Looking for new horizons

Notifications

Receive the new articles in your email

2022 © nepy

One single breath

Configure

Choose your own style

Color

Choose the perfect color mode for you


Navigation Position

Select a suitable navigation system


Vertical Navbar Style

Switch between styles for your vertical navbar

Customize