Untitled

 avatar
unknown
python
2 years ago
1.5 kB
4
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 get_first_input():
    num1 = input("What's the first number?: ")
    try:
        return float(num1)
    except:
        print("Invalid data, given number must be an integer or a float!")
        my_calculator()

def get_second_input():
    num2 = input("What's the next number?: ")
    try:
        return float(num2)
    except:
        print("Invalid data AGAIN, given number must be an integer or a float!")
        my_calculator()


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


    for symbol in operations:
        print(symbol)

    flag = True

    while flag:
        operation_symbol = input("Pick an operation: ")
        num2 = get_second_input()
        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...