Python Project – Simple Calculator
Master Python with 70+ Hands-on Projects and Get Job-ready - Learn Python
Program 1
#Simple calculator
# Function for Addtion
def add(x,y):
return(x+y)
def sub(x,y):
return(x-y)
def div(x,y):
if y != 0:
return x / y
else:
return "Cannot divide by zero"
def multiply(x,y):
return(x*y)
# Main
print("------------------Simple Calcultor-----------------")
print("1. Add \n2. Subtract \n3. Multiply \n4.Division")
print("----------------------------------------------------")
choice=int(input("Enter your choice(1,2,3,4)"))
num1=float(input("Enter first number"))
num2=float(input("Enter second number"))
if(choice==1):
print(add(num1,num2))
elif(choice==2):
print(sub(num1,num2))
elif(choice==3):
print(multiply(num1,num2))
elif(choice==4):
print(div(num1,num2))
else:
print("Invaliv choice")
Program 2
# Simple calculator
def add(x,y):
return(x+y)
def sub(x,y):
return(x-y)
def div(x,y):
if y != 0:
return x / y
else:
return "Cannot divide by zero"
def multiply(x,y):
return(x*y)
print("----------Simple Calculator---------------")
num1=float(input("Enter first number"))
num2=float(input("Enter second number"))
choice=input("Enter operator( + , - ,* , /)")
if(choice=='+'):
print(add(num1,num2))
elif(choice=='-'):
print(sub(num1,num2))
elif(choice=='*'):
print(multiply(num1,num2))
elif(choice=='/'):
print(div(num1,num2))
else:
print(" Invalid operator" )
Did you know we work 24x7 to provide you best tutorials
Please encourage us - write a review on Google

