unboundlocalerror local variable 'result' referenced before assignment
本问题已经有最佳答案,请猛点这里访问。
这个错误被问了很多次,但我不能通过阅读来解决我的问题。我的代码是:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | def show_preference_list_isolated(isolated): preference_list_isolated = { (240, 323): [(164, 108),(178, 422),(152, 77),(126, 431)], (396, 168): [(178, 422),(164, 108),(126, 431),(152, 77)], (288, 235): [(178, 422),(126, 431),(152, 77),(164, 108)], (88, 248): [(164, 108),(178, 422),(152, 77),(126, 431)], } if isolated in preference_list_isolated: result = preference_list_isolated[isolated] return result def begin_matching(isolated): preference_list_isolated = show_preference_list_isolated(isolated) print(preference_list_isolated) for relay in preference_list_isolated: .... |
在代码中,问题出在以下部分:
1 2 3 | if isolated in preference_list_isolated: result = preference_list_isolated[isolated] return result |
正如朱利安指出的,当
因此,要纠正这一点,您的一个选择可能是在
1 2 3 4 | result = None if isolated in preference_list_isolated: result = preference_list_isolated[isolated] return result |
或者,把
1 2 3 | if isolated in preference_list_isolated: result = preference_list_isolated[isolated] return result |