jeudi 2 septembre 2021

How to Make calculator With out if else?

I tried to make a calculator in python with out if else statements . i can't find how to fix a issue in present code

def add(n1, n2):
return n1+n2
def sub(n1, n2):
    return n1-n2
def mul(n1, n2):
    return n1*n2
def div(n1, n2):
    return n1/n2
def pow(n1, n2):
    return n1^n2
operations_and_functions= {
    "+":"add",
    "-":"sub",
    "*":"mul",
    "/":"div",
    "^":"pow",
}
num1 = int(input("What is your first number=> "))
num2 = int(input("What is your second number=> "))
for operations in operations_and_functions:
    print(operations)
***operation = str(input("What operation do you want to do=> "))
calculation = operations_and_functions[operation](num1,num2)***
answer = calculation(num1,num2)
print(f"{num1} {calculation} {num2} is equal to {answer}")

I can't convert this code" [operations_and_functionsoperation" into "add(num1,num2)"

Aucun commentaire:

Enregistrer un commentaire