How To Make A Simple Calculator in Python
Abhishek Hari
Posted on December 5, 2021
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: "))
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 '/' "))
Lastly for the inputs, we create num2
, i.e the second number the user needs to calculate.
num2 = int(input("❯ Enter The Second Number: "))
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
Subtraction
if operator == '-':
answer = num1 - num2
Multiplication
if operator == '*':
answer = num1 * num2
Division
if operator == '/':
answer = num1 / num2
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))
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:
Posted on December 5, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.