jack.math
0
Q:

how to make a 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


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
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
#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