Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- test_24_string = str(input("Enter your answer: "))
- test_24 = eval(test_24_string)
- >>> import numexpr
- >>> numexpr.evaluate('2 + 4.1 * 3')
- array(14.299999999999999)
- >>> numexpr.evaluate('17 / 3').item()
- 5.666666666666667
- >>> b = 10
- >>> numexpr.evaluate('exp(17) / b').item()
- 2415495.27535753
- >>> import ast
- >>> ast.literal_eval('1+2')
- 3
- >>> ast.literal_eval('import os')
- SyntaxError: invalid syntax
- >>> ast.literal_eval('exec(1+2)')
- ValueError: malformed node or string: <_ast.Call object at 0x0000023BDEADB400>
- >>> ast.literal_eval('1.2 * 2.3')
- ValueError: malformed node or string: <_ast.BinOp object at 0x0000023BDEF24B70>
- import operator
- import math
- _add, _sub, _mul = operator.add, operator.sub, operator.mul
- _truediv, _pow, _sqrt = operator.truediv, operator.pow, math.sqrt
- _sin, _cos, _tan, _radians = math.sin, math.cos, math.tan, math.radians
- _asin, _acos, _atan = math.asin, math.acos, math.atan
- _degrees, _log, _log10 = math.degrees, math.log, math.log10
- _e, _pi = math.e, math.pi
- _ops = {'+': (2, _add), '-': (2, _sub), '*': (2, _mul), '/': (2, _truediv),
- '**': (2, _pow), 'sin': (1, _sin), 'cos': (1, _cos), 'tan': (1, _tan),
- 'asin': (1, _asin), 'acos': (1, _acos), 'atan': (1, _atan),
- 'sqrt': (1, _sqrt), 'rad': (1, _radians), 'deg': (1, _degrees),
- 'ln': (1, _log), 'log': (1, _log10)}
- _okeys = tuple(_ops.keys())
- _consts = {'e': _e, 'pi': _pi}
- _ckeys = tuple(_consts.keys())
- def postfix(expression):
- """
- Evaluate a postfix expression.
- Arguments:
- expression: The expression to evaluate. Should be a string or a
- sequence of strings. In a string numbers and operators
- should be separated by whitespace
- Returns:
- The result of the expression.
- """
- if isinstance(expression, str):
- expression = expression.split()
- stack = []
- for val in expression:
- if val in _okeys:
- n, op = _ops[val]
- if n > len(stack):
- raise ValueError('not enough data on the stack')
- args = stack[-n:]
- stack[-n:] = [op(*args)]
- elif val in _ckeys:
- stack.append(_consts[val])
- else:
- stack.append(float(val))
- return stack[-1]
- In [2]: from postfix import postfix
- In [3]: postfix('1 2 + 7 /')
- Out[3]: 0.42857142857142855
- In [4]: 3/7
- Out[4]: 0.42857142857142855
Add Comment
Please, Sign In to add comment