Untitled

 avatar
unknown
python
2 years ago
1.4 kB
7
Indexable
from calc_logo import logo

# Calculator
def add(n1, n2):
    return n1 + n2

def subtract(n1, n2):
    return n1 - n2

def multiply(n1, n2):
    return n1 * n2

def divide(n1, n2):
    return n1 / n2

def exponentiation(n1, n2):
    return n1 ** n2

operations = {
    "+": add,
    "-": subtract,
    "*": multiply,
    "/": divide,
    "**": exponentiation

}

def my_calculator():
    print("Welcome to Marta's calculator!")
    print(logo)

    try:
        num1 = input("What's the first number?: ")
        num1 = float(num1)
    except:
        raise ValueError("Invalid data, given number must be an integer or a float!")

    for symbol in operations:
        print(symbol)

    flag = True

    while flag:
        operation_symbol = input("Pick an operation: ")
        try:
            num2 = float(input("What's the next number?: "))
        except:
            raise ValueError("Invalid data AGAIN, given number must be an integer or a float!")
            
        calculation_function = operations[operation_symbol]
        answer = calculation_function(num1, num2)
        print(f"{num1} {operation_symbol} {num2} = {answer}")

        if input(f"Type 'y' to continue calculating with {answer}, or type 'n' to start a new calc: ") == 'y':
            num1 = answer
        else:
            flag = False
            my_calculator()


my_calculator()
Editor is loading...