Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

howt to make caluclator in python

# 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


print("Select operation.")
print("1.Add")
print("2.Subtract")
print("3.Multiply")
print("4.Divide")

while True:
    # Take input from the user
    choice = input("Enter choice(1/2/3/4): ")

    # Check if choice is one of the four options
    if choice in ('1', '2', '3', '4'):
        num1 = float(input("Enter first number: "))
        num2 = float(input("Enter second number: "))

        if choice == '1':
            print(num1, "+", num2, "=", add(num1, num2))

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

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

        elif choice == '4':
            print(num1, "/", num2, "=", divide(num1, num2))
        break
    else:
        print("Invalid Input")
Comment

PREVIOUS NEXT
Code Example
Python :: print nested list in new lines 
Python :: py-trello add card 
Python :: python program to find fibonacci series using function recursion loop 
Python :: python inheritance remove an attribute 
Python :: how to install cuda in anaconda 
Python :: list to string python 
Python :: how to delete the last item in a list python 
Python :: append one column pandas dataframe 
Python :: delete row from dataframe python 
Python :: clear pygame screen 
Python :: create folder python 
Python :: pandas convert all string columns to lowercase 
Python :: standard module 
Python :: how to delete everything on a file python 
Python :: generate random integer matrix python 
Python :: django update increment 
Python :: how to change the window colour in pygame 
Python :: python get all methods of object 
Python :: cv2 add circle to image 
Python :: drop rows with certain values pandas 
Python :: how to calculate mean in python 
Python :: pil image shape 
Python :: install django windows 
Python :: python remove duplicates from list 
Python :: python your mom 
Python :: python reduce function to sum array 
Python :: python matplotlib hist set axis range 
Python :: python if else variable assignment 
Python :: python reverse string 
Python :: read csv uisng pandas 
ADD CONTENT
Topic
Content
Source link
Name
8+9 =