In a function, I need to perform some logic that requires me to call a function inside a function. What I did with this, like:
def dfs(problem):
stack.push(bache)
search(root)
while stack.isEmpty() != 0:
def search(vertex):
closed.add(vertex)
for index in sars:
stack.push(index)
return stack
In the function, dfs, I am using search(root), is this is the correct way to do it?
I am getting an error: local variable ‘search’ referenced before assignment
There are many mysterious bug-looking aspects in your code. The wrong order of definition (assuming you do need the
searchfunction to be a nested one) and the syntax error from the emptywhileloop have already been observed, but there are more…:what’s
bache, what’sstack, what’sroot? If they’re all global variables, then you’re overusing globals — and apparently nowhere ever using the argumentproblem(?!).what’s this weird-looking method
isEmpty? IOW, what type isstack(clearly not a Python list, and that’s weird enough, since they do make excellent LIFO stacks;-)…? And what’s ever going to make it empty…?…don’t tell me:
closedis yet another global? Presumably a set? (I remember from a few of your Qs back that you absolutely wanted to have acloseddict, not set, even though I suggested that as a possibility……and what’s
sars?!what a weird “loop” — one that executes exactly once, altering a global variable, and then immediately returns that global variable (?) without doing any of the other steps through the loop. Even if this is exactly what you mean (push the first item of
sars, period) I don’t recommend hiding it in a pseudo-loop — it seriously looks like a mystery bug just waiting to happen;-).