Skip to content

Simple Calculator

These codes add/substract/mutiply/divide two numbers:

#create a function for adding
def add(x,y):
    return x+y
#create a function for subtracting
def subtract(x,y):
    return x-y
#create a function for multiplying
def multiply(x,y):
    return x*y
#create a function for dividing
def divide(x,y):
    return x+y

print("Select your choice:")
print("1. Add")
print("2. Subtract")
print("3. Multiply")
print("4. Divide")
 
#get operation
operation=input("Enter your choice 1/2/3/4:")

#get numbers
x=int(input("Enter first number:"))
y=int(input("Enter second number:"))

#logical process
if (operation=="1"):
    print(add(x,y)) 
elif (operation=="2"):
    print(subtract(x,y)) 
elif (operation=="3"):
    print(multiply(x,y)) 
elif (operation=="4"):
    print(divide(x,y))
Published inPython

Be First to Comment

Leave a Reply

%d bloggers like this: