Saturday, May 30, 2020

Python Calculator

# Program make a simple calculator

# This function adds two numbers
def add(x, y):
   return x + y

# This function subtracts two numbers
def subtract(x, y):
   return x - y

# This function multiplies two numbers
def multiply(x, y):
   return x * y

# This function divides two numbers
def divide(x, y):
   return x / y

Repeat ='Y'

while (Repeat=='Y' or Repeat=='y'):
    print("SELECT OPERATION.")
    print("    + Add")
    print("    - Subtract")
    print("    * Multiply")
    print("    / Divide")

    # Take input from the user
    choice = input("Your choice is : ")

    num1 = float(input("Enter first number:"))
    num2 = float(input("Enter second number: "))
    if choice == '+':
        print(num1,"+",num2,"=", add(num1,num2))

    elif choice == '-':
        print(num1,"-",num2,"=", subtract(num1,num2))
 
    elif choice == '*':
        print(num1,"*",num2,"=", multiply(num1,num2))

    elif choice == '/':
        print(num1,"/",num2,"=", divide(num1,num2))
    else:
        print("your choice is invalid")
       
    Repeat=input("Do you want to continue Enter Y/N")

No comments:

Post a Comment

Search

Python Calculator

# Program make a simple calculator # This function adds two numbers def add(x, y):    return x + y # This function subtracts two numb...