最近在学习python的过程中遇到了一些问题,希望各位专家指教!
题目是这样的:

以下是我的代码:
def right_left(s):
mid = len(s) // 2
for i in range(len(s)):
if i <= mid + 1:
x = s[i] * 2
x
else:
y = s[i] * 2
y
return y + x
print(right_left('llkk'))

以下是报错信息:
发生异常: UnboundLocalError
cannot access local variable 'y' where it is not associated with a value
File "D:\code\right_left().py", line 11, in right_left
return y + x
^
File "D:\code\right_left().py", line 13, in
print(right_left(''))
^^^^^^^^^^^^^^^^^^
UnboundLocalError: cannot access local variable 'y' where it is not associated with a value

翻译成中文就是:
发生异常:UnboundLocalError
无法访问未与值关联的局部变量“y”
文件“D:\code\right_left().py”,第 11 行,位于 right_left
返回 y + x
^
文件“D:\code\right_left().py”,第 13 行,位于
print(right_left(''))
^^^^^^^^^^^^^^^^^^^
UnboundLocalError:无法访问未与值关联的局部变量“y”
可以注意在截图中,显示定义的变量中并没有y,可在代码的第九行,明确定义了y,就和y一样,可是x被成功定义而y没有,请问各位专家这是为什么呢?