s = input(': ') for word in forbidden: if word in s: print('{} is not allowed'.format(word)) ok = False

if s and ok: try: print(eval(s)) except: print(sys.exc_info())

and my example outputs:

: __import__('subprocess').Popen('ls')imp is not allowedsubprocess is not allowed__import__ is not allowedopen is not allowedimport is not allowedor is not allowed: : 1/0(<class 'ZeroDivisionError'>, ZeroDivisionError('division by zero',), <traceback object at 0x7fa1f78e6ab8>): 1+12: 1+1+1*1012: 2*2(2)(<class 'TypeError'>, TypeError("'int' object is not callable",), <traceback object at 0x7fa1f78e6a28>): a + 1(<class 'NameError'>, NameError("name 'a' is not defined",), <traceback object at 0x7fa1f78c5ef0>): a = 1(<class 'SyntaxError'>, SyntaxError('invalid syntax', ('<string>', 1, 3, 'a = 1')), <traceback object at 0x7fa1f78c5ef0>): a + 1(<class 'NameError'>, NameError("name 'a' is not defined",), <traceback object at 0x7fa1f78e6a28>): class Test:;self.a=0as is not allowedclass is not allowed: open('test.txt,'w').write('hello')test is not allowedopen is not allowed: open('test.txt).read()test is not allowedre is not allowedopen is not allowed: open('test.txt').read()test is not allowedre is not allowedopen is not allowed

well 'test' being in there because i have a module name test in home directory.