How To Make a Simple Calculator on Phone using Python?

Welcome back my Otakus,

In this blog, I am going to teach you how you can make a simple calculator on Phone using Python that you will be able to use anytime anywhere. So let’s get started already.

In this blog we are going to make a simple python calculator using def and if-elif-else functions.

Let’s Start….

First you need to define functions for your calculator like this.

def add(a,b):
	print(a+b)
	
def sub(a,b):
	print(a-b)
	
def mul(a,b):
	print(a*b)
	
def div(a,b):
	print(a/b)

Now, the next step is to write commands for taking values from the user on which the user want to perform calculations.

For that, we have to take input from the user and convert it to float even if the input is whole number of decimal number.

Now for that the code will be :

a=float(input("Enter the first number = "))
b=float(input("Enter the second number = "))

After writing code for the input, now we have to write code for the taking input from the user that which operations he want to perform.

c=input("Enter the symbol of operation you want to perform +,-,*,/  ")

Now the next and final step is to write code for different operators using conditional statements for the operations to be performed

if c=="+":
	add(a,b)
elif c=="-":
  	sub(a,b)
  	
elif c=="*":
  	mul(a,b)
 
elif c=="/":
  	div(a,b)
  	
else:
  	print("Enter Valid Symbol")

Now let’s have a look at the complete code:

def add(a,b):
	print(a+b)
	
def sub(a,b):
	print(a-b)
	
def mul(a,b):
	print(a*b)
	
def div(a,b):
	print(a/b)
	
a=float(input("Enter the first number = "))
b=float(input("Enter the second number = ")) 
c=input("Enter the symbol of operation you want to perform +,-,*,/  ")
if c=="+":
	add(a,b)
elif c=="-":
  	sub(a,b)
  	
elif c=="*":
  	mul(a,b)
 
elif c=="/":
  	div(a,b)
  	
else:
  	print("Enter Valid Symbol")

It’s time to run our program and see the output:

You can save this code file as calculator.py and can use it from anywhere which runs the python code.

I hope you had loved reading this blog.


Leave a Reply

Up ↑

Discover more from JD Bots

Subscribe now to keep reading and get access to the full archive.

Continue reading