Ad

Eval is unsafe, avoid using it!

Code
Diff
  • def Calculator(sign: str, n1: int, n2: int):
        match(sign):
            case "+":
                return n1+n2
            case "-":
                return n1-n2
            case "*":
                return n1*n2
            case "/":
                if n2==0:
                    return 0
                return n1/n2
            case _:
                return 0
    • def Calculator(expression):
    • try:
    • return eval(expression)
    • except:
    • return 0
    • def Calculator(sign: str, n1: int, n2: int):
    • match(sign):
    • case "+":
    • return n1+n2
    • case "-":
    • return n1-n2
    • case "*":
    • return n1*n2
    • case "/":
    • if n2==0:
    • return 0
    • return n1/n2
    • case _:
    • return 0