在<图解算法>的第六章 广度优先搜索中,6.5进行算法实现时,报如下错误:
UnboundLocalError: local variable 'search_queue' referenced before assignment
源代码(书本是python2.x代码,转换成python3.x格式)如下:
from collections import deque
graph = {}
graph['you'] = ['alice', 'bob', 'claire']
graph['bob'] = ['anuj', 'peggy']
graph['alice'] = ['peggy']
graph['claire'] = ['thom', 'jonny']
graph['anuj'] = []
graph['peggy'] = []
graph['thom'] = []
graph['jonny'] = []
search_queue = deque()
search_queue += graph['you']
while search_queue:
person = search_queue.popleft()
if person_is_seller(person):
print(person + ' is a mango seller!')
return True
else:
search_queue += graph[person]
return False
def person_is_seller(name):
return name[-1] == 'm'
按照下面这位博主所总结的第3条(先mark一下,之后在慢慢研究,感谢这位大哥):
-------------------
◆全局变量报错:UnboundLocalError: local variable 'l' referenced before assignment
<