代码块中用的 python 语言,以下写法在解释器中运行正常的,可以找到"BGX0004"对应的下一个字符串,但是在代码块中就提示:赋值前引用了局部变量”next_string“
my_list = ["BGX0004", "BGX0003", "BGX0002", "BGX0011", "BGX0001"]
target_string = "BGX0004"
index = None
for i, item in enumerate(my_list):
if item == target_string:
if index is None or my_list[index] != target_string:
index = i
if index is not None and index < len(my_list)-1:
next_string = my_list[index+1]
print(next_string)
else:
print(target_string)