unboundlocalerror local variable 'result' referenced before assignment
Question:
This error was asked many times but I can’t solve my problem by reading them.
My code is:
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:
....
Answers:
In your code, the problem lies in the section :
if isolated in preference_list_isolated:
result = preference_list_isolated[isolated]
return result
As Julien pointed out, what happens when isolated
is not found in preference_list_isolated
? At that point it never enters the if loop
, and since result = ...
is never executed, there is no assignment to result
. So when you try returning result
, it will throw up an Error as at that point it would not be defined at all.
So, to rectify that, your one option maybe to assign it with a placeholder None
before the if loop
, like so :
result = None
if isolated in preference_list_isolated:
result = preference_list_isolated[isolated]
return result
Or, put the return
statement in the if loop
, like so:
if isolated in preference_list_isolated:
result = preference_list_isolated[isolated]
return result
This error was asked many times but I can’t solve my problem by reading them.
My code is:
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:
....
In your code, the problem lies in the section :
if isolated in preference_list_isolated:
result = preference_list_isolated[isolated]
return result
As Julien pointed out, what happens when isolated
is not found in preference_list_isolated
? At that point it never enters the if loop
, and since result = ...
is never executed, there is no assignment to result
. So when you try returning result
, it will throw up an Error as at that point it would not be defined at all.
So, to rectify that, your one option maybe to assign it with a placeholder None
before the if loop
, like so :
result = None
if isolated in preference_list_isolated:
result = preference_list_isolated[isolated]
return result
Or, put the return
statement in the if loop
, like so:
if isolated in preference_list_isolated:
result = preference_list_isolated[isolated]
return result