DEV Community

Mohammad Alsuwaidi
Mohammad Alsuwaidi

Posted on

A simple python calculator

i made a simple python calculator using while loop

# simple calculator


def add(x, y):
    return x + y


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


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


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


print("Select operation.")
print("( + )for Add")
print("( - ) for Subtract")
print("( * ) for Multiply")
print("( / ) for Divide")

while True:
    choice = raw_input("Enter the operation: ")
    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, "=", (sub(num1, num2))
        elif choice == "*":
            print num1, "*", num2, "=", (mul(num1, num2))
        elif choice == '/':
            print num1, "/", num2, "=", (div(num1, num2))
    n12 = raw_input("Do u want to calculate another equation (y/n): ")
    if n12 == 'y' or n12 == 'Y':
        continue
    else:
        break


Enter fullscreen mode Exit fullscreen mode

Top comments (0)