DEV Community

Cover image for How To Make A Simple Calculator in Python
Abhishek Hari
Abhishek Hari

Posted on • Edited on

How To Make A Simple Calculator in Python

If you're new to Python, here is a simple project for you! Create an extremely simple Calculator to improve your skills! In this article, I will show you how to make one.

Making The Inputs

Here, we need to create three inputs for the user to input their desired values.

First we create num1, i.e the first number the user needs to calculate.

 num1 = int(input("❯ Enter The First Number: "))
Enter fullscreen mode Exit fullscreen mode

Second, we create operator so that the user can input the operator. For example, if the user need to multiply, they can simply enter *.

operator = (input("❯ Enter '+', '-', '*' or '/' "))
Enter fullscreen mode Exit fullscreen mode

Lastly for the inputs, we create num2, i.e the second number the user needs to calculate.

num2 = int(input("❯ Enter The Second Number: "))
Enter fullscreen mode Exit fullscreen mode

Do not forget to add int(integer) before the inputs!

Writing Logic

Now we are going to code the logic behind that calculator.

If Statements

Using if statements, we are going to code so that the calculator respectively adds, subtracts, multiplies or divides.

Addition

if operator == '+':
    answer = num1 + num2
Enter fullscreen mode Exit fullscreen mode

Subtraction

if operator == '-':
    answer = num1 - num2
Enter fullscreen mode Exit fullscreen mode

Multiplication

if operator == '*':
    answer = num1 * num2
Enter fullscreen mode Exit fullscreen mode

Division

if operator == '/':
    answer = num1 / num2
Enter fullscreen mode Exit fullscreen mode

That's it for the logic!

The Final Step

That last thing you need to do is print it!

print("❯ The Answer Is " + str(answer))
Enter fullscreen mode Exit fullscreen mode

Again make sure str(string) is there!

And that's it, the calculator has been successfully completed!
Congratulations on this project and happy learning!

Here is the finished product:

Finished Product

Top comments (0)