Wqwqw Wqefhgiff
Мастер
(1247),
на голосовании
3 месяца назад
functions = {} vars = {}
def define_function(name, body): functions[name] = body
def call_function(name, *arguments): if name in functions: return functions[name](*arguments) else: print(f"Error: Function '{name}' is not defined")
def interpret(code): lines = code.strip().splitlines() for line in lines: parts = line.split("(") func_name = parts[0].strip() args_str = parts[1].strip(")").strip() if len(parts) > 1 else "" args = [arg.strip() for arg in args_str.split(",")] if args_str else [] call_function(func_name, *args)
def include_lib(lib): path = f'libs/{lib}.py' with open(path) as lib_file: content = lib_file.read() exec(content)
define_function('print_var', lambda a: print(vars.get(a))) define_function('print', lambda a: print(a)) define_function('set_int', lambda a, b: vars.update({a: int(b)})) define_function('set_str', lambda a, b: vars.update({a: b})) define_function('set_float', lambda a, b: vars.update({a: float(b)})) define_function('set_bool', lambda a, b: vars.update({a: bool(b)})) define_function('set_list', lambda a, b: vars.update({a: list(b)}))
code = ''' set_list(myList, [1,2,3]) '''
interpret(code)
Исправьте ошибку:
Traceback (most recent call last): File "c:\Users\dns\Desktop\Funct\main.py", line 40, in interpret(code) File "c:\Users\dns\Desktop\Funct\main.py", line 20, in interpret call_function(func_name, *args) File "c:\Users\dns\Desktop\Funct\main.py", line 9, in call_function return functions[name](*arguments) ^^^^^^^^^^^^^^^^^^^^^^^^^^^ TypeError: () takes 2 positional arguments but 4 were given
Python 3.12