calculator using python code example

Example 1: simple python calculator

#Store number variables for the two numbers

num1 = input('Enter first number: ')
num2 = input('Enter second number: ')

#the sum of the two numbers variable
sum = float(num1) + float(num2)
sum2 = float(num1) - float(num2)
sum3 = float(num1) * float(num2)
sum4 = float(num1) / float(num2)

#what operator to use
choice = input('Enter an operator, + = addition, - = subtraction, * = multiplication and / = division: ')
#different sums based on the operators
if choice == '+':
  print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))

  if choice == '-':
    print('The sum of {0} and {1} is {2}'.format(num1, num2, sum2))

if choice == '*':
    print('The sum of {0} and {1} is {2}'.format(num1, num2, sum3))

if choice == '/':
    print('The sum of {0} and {1} is {2}'.format(num1, num2, sum4))

Example 2: python calculator

num_one = int(input("Enter 1st number: "))

op = input("Enter operator: ")

num_two = int(input("Enter 2nd number: "))

if op == "+":
    print(num_one + num_two)
elif op == "-":
    print(num_one - num_two)
elif op == "*" or op == "x":
    print(num_one * num_two)
elif op == "/":
    print(num_one / num_two)

Example 3: calculator code

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!")

Example 4: calculator in python

Select operation.
1.Add
2.Subtract
3.Multiply
4.Divide
Enter choice(1/2/3/4): 3
Enter first number: 15
Enter second number: 14
15.0 * 14.0 = 210.0

Example 5: python calculator

import time
def cal():
    def add(x, y):
        print(x + y)

    def sub(x, y):
        print(x - y)

    def mul(x, y):
        print(x * y)

    def div(x, y):
        print(x // y)

    while True:
        print('Adding?(+) Subtracting?(-) Multiplying?(*) Dividing?(/)')
        answer = input('')
        if answer == "+":
            try:
                x = float(input('First number! '))
                y = float(input('Second Number! '))
            except Exception as e:
                print(f'Error: {e}')
                time.sleep(5)
                return
            add(x, y)
        elif answer == "-":
            try:
                x = float(input('First number! '))
                y = float(input('Second Number! '))
            except Exception as e:
                print(f'Error: {e}')
                time.sleep(5)
                return
            sub(x, y)
        elif answer == "*":
            try:
                x = float(input('First number! '))
                y = float(input('Second Number! '))
            except Exception as e:
                print(f'Error: {e}')
                time.sleep(5)
                return
            mul(x, y)
        elif answer == "/":
            try:
                x = float(input('First number! '))
                y = float(input('Second Number! '))
            except Exception as e:
                print(f'Error: {e}')
                time.sleep(5)
                return
            div(x, y)
        else:
            print('Error. Enter a valid input!')


cal()

Tags:

Html Example