Арсен Жуматай,304 UnboldCalc 12776
Всеволод Оплачко, 404 группа КФ UnboldCalc 12974
n1class Calculator:n1class ExpressionCalculator:
22
3    def __init__(self):3    def __init__(self):
n4        self.variables = {}n4        self.storage = {}
5        self.allowed_namespaces = {'__builtins__': {}}5        self.safe_env = {'__builtins__': {}}
66
n7    def process_input(self, line):n7    def handle_input(self, command):
8        line = line.strip()8        command = command.strip()
9        if not line or line.startswith('#'):9        if not command or command.startswith('#'):
10            return None10            return None
n11        if '=' in line:n11        if '=' in command:
12            identifier, expr = map(str.strip, line.split('=', 1))12            variable, expression = map(str.strip, command.split('=', 1))
13            return self.assign(identifier, expr)13            return self.set_variable(variable, expression)
14        return self.evaluate_expression(line)14        return self.evaluate(command)
1515
n16    def assign(self, identifier, expr):n16    def set_variable(self, variable, expression):
17        if not identifier.isidentifier():17        if not variable.isidentifier():
18            return 'Assignment error'18            return 'Assignment error'
19        try:19        try:
n20            self.variables[identifier] = eval(expr, self.allowed_namespan20            self.storage[variable] = eval(expression, self.safe_env, sel
>ces, self.variables)>f.storage)
21        except SyntaxError:21        except SyntaxError:
22            return 'Syntax error'22            return 'Syntax error'
23        except NameError:23        except NameError:
24            return 'Name error'24            return 'Name error'
25        except Exception:25        except Exception:
26            return 'Runtime error'26            return 'Runtime error'
27        return None27        return None
2828
n29    def evaluate_expression(self, expr):n29    def evaluate(self, expression):
30        try:30        try:
n31            result = eval(expr, self.allowed_namespaces, self.variables)n31            result = eval(expression, self.safe_env, self.storage)
32            return result32            return result
33        except SyntaxError:33        except SyntaxError:
34            return 'Syntax error'34            return 'Syntax error'
35        except NameError:35        except NameError:
36            return 'Name error'36            return 'Name error'
37        except Exception:37        except Exception:
38            return 'Runtime error'38            return 'Runtime error'
n39calculator = Calculator()n39calc = ExpressionCalculator()
40while True:40while True:
n41    line = input().strip()n41    cmd = input().strip()
42    if not line:42    if not cmd:
43        break43        break
t44    result = calculator.process_input(line)t44    output = calc.handle_input(cmd)
45    if result is not None:45    if output is not None:
46        print(result)46        print(output)
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op