0
Q:

how to make a calculator 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")
1
def calculate():
    operation = input('''
Please type in the math operation you would like to complete:
+ for addition
- for subtraction
* for multiplication
/ for division
''')

    number_1 = int(input('Please enter the first number: '))
    number_2 = int(input('Please enter the second number: '))

    if operation == '+':
        print('{} + {} = '.format(number_1, number_2))
        print(number_1 + number_2)

    elif operation == '-':
        print('{} - {} = '.format(number_1, number_2))
        print(number_1 - number_2)

    elif operation == '*':
        print('{} * {} = '.format(number_1, number_2))
        print(number_1 * number_2)

    elif operation == '/':
        print('{} / {} = '.format(number_1, number_2))
        print(number_1 / number_2)

    else:
        print('You have not typed a valid operator, please run the program again.')

    # Add again() function to calculate() function
    again()

def again():
    calc_again = input('''
Do you want to calculate again?
Please type Y for YES or N for NO.
''')

    if calc_again.upper() == 'Y':
        calculate()
    elif calc_again.upper() == 'N':
        print('See you later.')
    else:
        again()

calculate()
0
x = input("first number")
y = input("second number")
z = input("do you want to multiply, minus, divide or add? (x,-,/,+)")

y = int(y)
x = int(x)

if z == "+":
    print (x + y)

if z == "/":
    print (x / y)

if z == "x":
    print (x * y)

if z == "-":
    print (x - y)

else:
    print("use x,-,/ or + next time!")
2
def mutiply (x):
    return 5*x
o = mutiply(10)
print(o)
3
# This will be one of the most advanced results you will find.

# We will be using classes and simple operators like +,-,*,/

class Calculator:
  def addition(a,b):
    return a + b

  def subtraction(a,b):
    if a<b:
      return b - a
    else:
      return a - b

  def multiplication(a,b):
    return a * b

  def division(a,b):	
    if a<b:
      return b / a
    else:
      return a / b

# You can do this in terminal.
<C:/Users/username>python
>>> from main import Calculator
>>> result = Calculator.[addition|subtraction|multiplication|division](anyNumber, anyNumber)
>>> print(result)
0
# Python program for simple calculator 
  
# Function to add two numbers  
def add(num1, num2): 
    return num1 + num2 
  
# Function to subtract two numbers  
def subtract(num1, num2): 
    return num1 - num2 
  
# Function to multiply two numbers 
def multiply(num1, num2): 
    return num1 * num2 
  
# Function to divide two numbers 
def divide(num1, num2): 
    return num1 / num2 
  
print("Please select operation -\n" \ 
        "1. Add\n" \ 
        "2. Subtract\n" \ 
        "3. Multiply\n" \ 
        "4. Divide\n") 
  
  
# Take input from the user  
select = input("Select operations form 1, 2, 3, 4 :") 
  
number_1 = int(input("Enter first number: ")) 
number_2 = int(input("Enter second number: ")) 
  
if select == '1': 
    print(number_1, "+", number_2, "=", 
                    add(number_1, number_2)) 
  
elif select == '2': 
    print(number_1, "-", number_2, "=", 
                    subtract(number_1, number_2)) 
  
elif select == '3': 
    print(number_1, "*", number_2, "=", 
                    multiply(number_1, number_2)) 
  
elif select == '4': 
    print(number_1, "/", number_2, "=", 
                    divide(number_1, number_2)) 
else: 
    print("Invalid input") 
5
# define the function
def calculate():
    print('Please choose your operator adding(+) subtracting(-) multiplying(*) didviding(/) for power(**) for module(%)')

    number_1 = int(input("Enter your first digit: "))
    operation = input("Enter your operator: ")
    number_2 = int(input("Enter your first digit: "))


# adding
    if operation == '+':
        print('{} + {} = '.format(number_1, number_2))
        print(number_1 + number_2)
        print("You were adding.\n")
        print("How do I know that I'm a smart progammer ;)\n")

# subtracting
    elif operation == '-':
        print('{} - {} = '.format(number_1, number_2))
        print(number_1 - number_2)
        print("You were subtracting.\n")
        print("How do I know that I'm a smart progammer ;)\n")

# mulitplaying
    elif operation == '*':
        print('{} * {} = '.format(number_1, number_2))
        print(number_1 * number_2)
        print("You were mulitplaying.")
        print("How do I know that I'm a smart progammer ;)\n")

# dividing
    elif operation == '/':
        print('{} / {} = '.format(number_1, number_2))
        print(number_1 / number_2)
        print("You were dividing.\n")
        print("How do I know that I'm a smart progammer ;)\n")

# for power
    elif operation == '**':
        print('{} ** {} = '.format(number_1, number_2))
        print(number_1 ** number_2)
        print("You were using for power.\n")
        print("How do I know that I'm a smart progammer ;)\n")

# module
    elif operation == '%':
        print('{} % {} = '.format(number_1, number_2))
        print(number_1 % number_2)
        print("You were using module.\n")
        print("How do I know that I'm a smart progammer ;)\n")

    # if they get a error
    else:
        print("Your number you have typed is invalid, please restart your program!")
    # add again() here as a function outside the calculate()
    again()


def again():
    cal_again = input("Do you want to calculate again? Y = yes or N = no: ")

    # Taking user input
    if cal_again.upper() == 'Y':
        calculate()
    elif cal_again.upper() == 'N':
        print('Leave kid ;-;')
    else:
        again()


def welcome():
    print("Welcome to my calculator made by Pepa pig lol made in python :D")


# use calculate() for the function
welcome()
calculate()
0
#Calculator

import time

def add():
  n1 = input('Enter a number: ')
  n2 = input('Enter another number: ')
  time.sleep(1)
  print(int(n1)+int(n2))
  
def multiply():
  n1 = input('Enter a number: ')
  n2 = input('Enter another number: ')
  time.sleep(1)
  print(int(n1)*int(n2))
    
def divide():
  n1 = input('Enter a number: ')
  n2 = input('Enter another number: ')
  time.sleep(1)
  print(int(n1)int(n2))
  
def subtract():
  n1 = input('Enter a number: ')
  n2 = input('Enter another number: ')
  time.sleep(1)
  print(int(n1)-int(n2))
  
run = True
while run:
  ans = input('Select an opreation: ')
  if(ans=='Addition'):
    add()
    
  if(ans=='Divide'):
    divide()
    
  if(ans=='Multiply'):
    multiply()
    
  if(ans=='Subtract'):
    subtract()
  
  
  
0
def mutiply (x):
    return _*x
o = mutiply(_)
print(o)
-1

New to Communities?

Join the community