Ad
Mathematics
Fundamentals

simple is better that complex, right?

Code
Diff
  • def calculator(a: (int | float) = 0, b: str = '+', c: (int | float) = 0, *, exceptions: bool = False, what: bool = False, using_exec: bool = False):
        try:
            return eval(str(a)+str(b)+str(c)) if not using_exec else exec(str(a)+str(b)+str(c))
        except ZeroDivisionError as e:
            if exceptions: raise e
            else: return float('inf')
        except Exception as e:
            if exceptions: raise e
            else: return float('NaN')
        finally:
            if what: return calculator(a^c, '/', a | c + 1, what=True)
            
    • def calculator(a,b,c):
    • try: return eval(str(a)+str(b)+str(c))
    • except ZeroDivisionError: return float('inf')
    • def calculator(a: (int | float) = 0, b: str = '+', c: (int | float) = 0, *, exceptions: bool = False, what: bool = False, using_exec: bool = False):
    • try:
    • return eval(str(a)+str(b)+str(c)) if not using_exec else exec(str(a)+str(b)+str(c))
    • except ZeroDivisionError as e:
    • if exceptions: raise e
    • else: return float('inf')
    • except Exception as e:
    • if exceptions: raise e
    • else: return float('NaN')
    • finally:
    • if what: return calculator(a^c, '/', a | c + 1, what=True)
Mathematics
Fundamentals

bruh...

i dunno how to use kumite, i think, i just need to rewrite this code...

what?

Code
Diff
  • def calculator(sign:str = '+', n1:(float | int) = 0, n2:(float | int) = 0, exceptions_allowed: bool = False):
        """The simplest calculator ever.
        First argument is sign, +, -, *, /, ** and % supported. Default is +.
        The next two arguments is n1 and n2. Integers, Floats and Complex numbers supported. Their defaults is 0.
        
        Other arguments is options.
        exceptions_allowed - if True, will return exception instead of 0 when error occurs. Default is False."""
        if not isinstance(sign, str):
            if not exceptions_allowed: return 0 
            else: raise TypeError('Type of the sign must be string.')
        if not isinstance(n1, (int, float, complex)): 
            if not exceptions_allowed: return 0 
            else: raise TypeError('Type of the first number must be int, float or complex.')
        if not isinstance(n2, (int, float, complex)): 
            if not exceptions_allowed: return 0 
            else: raise TypeError('Type of the second number must be int, float or complex.')
        if sign not in "+-*/**%": 
            if not exceptions_allowed: return 0 
            else: raise TypeError('Sign must be +, -, *, /, ** or %.')
        if sign == "/" and n2 == 0:
            if not exceptions_allowed: return float('inf') #mathematically this is better, than x/0=0
            else: raise ZeroDivisionError('Can\'t divide by zero.')
        return eval(str(n1) + sign + str(n2)) #using universal and optimized, but dangerous and bugged solution
    • def Calculator(*args):
    • def calculator(sign:str = '+', n1:(float | int) = 0, n2:(float | int) = 0, exceptions_allowed: bool = False):
    • """The simplest calculator ever.
    • First argument is sign, +, -, *, /, ** and % supported. Default is +.
    • The next two arguments is n1 and n2. Integers, Floats and Complex numbers supported. Their defaults is 0.
    • if not (str(args[0]) in "+-*/" and len(args) == 3):
    • return 0
    • sign, n1, n2 = args
    • Other arguments is options.
    • exceptions_allowed - if True, will return exception instead of 0 when error occurs. Default is False."""
    • if not isinstance(sign, str):
    • if not exceptions_allowed: return 0
    • else: raise TypeError('Type of the sign must be string.')
    • if not isinstance(n1, (int, float, complex)):
    • if not exceptions_allowed: return 0
    • else: raise TypeError('Type of the first number must be int, float or complex.')
    • if not isinstance(n2, (int, float, complex)):
    • if not exceptions_allowed: return 0
    • else: raise TypeError('Type of the second number must be int, float or complex.')
    • if sign not in "+-*/**%":
    • if not exceptions_allowed: return 0
    • else: raise TypeError('Sign must be +, -, *, /, ** or %.')
    • if sign == "/" and n2 == 0:
    • return 0
    • return n1 + n2 if sign == "+" else n1 - n2 if sign == "-" else n1 * n2 if sign == "*" else n1 / n2 if sign == "/" else 0
    • if not exceptions_allowed: return float('inf') #mathematically this is better, than x/0=0
    • else: raise ZeroDivisionError('Can\'t divide by zero.')
    • return eval(str(n1) + sign + str(n2)) #using universal and optimized, but dangerous and bugged solution