23
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.
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!
Now we are going to code the logic behind that calculator.
Using if
statements, we are going to code so that the calculator respectively adds, subtracts, multiplies or divides.
if operator == '+':
answer = num1 + num2
if operator == '-':
answer = num1 - num2
if operator == '*':
answer = num1 * num2
if operator == '/':
answer = num1 / num2
That's it for the logic!
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:
23