python calculator

Do you want to know the details regarding “python calculator”. If yes, you’re in the correct tutorial.

python calculator

def calculate(a, b, formula):
  if formula == '+':
    return a + b
  elif formula == '-':
    return a - b
  elif formula == '*':
    return a * b
  elif formula == '/':
    return a / b
  else:
    print('Just Leave!')
    return 'Closing program...'

print('Choose a number')
a = float(input())
print('Choose a second number')
b = float(input())
print('Do you want to * / - or + ?')
formula = input()
answer = calculate(a, b, formula)
print(answer)
from whiteCalculator import Calculator
c = Calculator()
print(c.run("1+8(5^2)"))
# Output: 201
print(c.run("9Ans"))
# Output: 1809
print("Enter Your Choice 1(Add)/2(Sub)/3(Divide)/4(Multiply)")
num = int(input())
if num == 1:
    print("Enter Number 1 : ")
    add1  = int(input())
    print("Enter Number 2 : ")
    add2 = int(input())
    sum = add1 + add2
    print("The Sum Is ", sum)
elif num == 2:
    print("Enter Number 1 : ")
    sub1  = int(input())
    print("Enter Number 2 : ")
    sub2 = int(input())
    difference = sub1 - sub2
    print("The Difference Is ", difference)
elif num == 3:
    print("Enter Number 1 : ")
    div1  = float(input())
    print("Enter Number 2 : ")
    div2 = float(input())
    division = div1 / div2
    print("The Division Is ", division)
elif num == 4:
    print("Enter Number 1 : ")
    mul1 = int(input())
    print("Enter Number 2 : ")
    mul2 = int(input())
    multiply = mul1 * mul2
    print("The Difference Is ", multiply)
else:
    print("enter a valid Number")
 
 #Simple faulty 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("Add = +")
print("Subtract = -")
print("Multiply = *")
print("Divide = /")

while True:
    # Take input from the user
    choice = input("Enter choice(+ or - or * or /): ")

    # Check if choice is one of the four options
    if choice in ('+', '-', '*', '/'):
        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))
        break
    else:
        print("Invalid Input")
print("Choose operator (+,-,*,/):")
mode = input()
print("Choose first int:")
x = int(input())
print("Choose second int:")
y = int(input())

print("Your result:")
if mode == "+":
    print(x+y)
elif mode == "-":
    print(x-y)
elif mode == "*":
    print(x*y)
elif mode == "/":
    print(x/y)
print('Calculator')
input_1 = input('First Number? ')
input_2 = input('Second Number? ')

try:
    print(f'input_1 + input_2 is float(input_1) + float(input_2)')
    print(f'input_1 - input_2 is float(input_1) - float(input_2)') 
    print(f'input_1 X input_2 is float(input_1) * float(input_2)')
    print(f'input_1 / input_2 is float(input_1) // float(input_2)')
except Exception as e:
    print(f'ERROR: e')

Final Words

I hope this tutorial helps you to know about “python calculator”. If you have any questions regarding this post please let us know via the comment section. Share this tutorial with your friends and family via social networks.

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coder Diksha. Here, I post about programming to help developers.

Share on:

Leave a Comment