Exception has occurred: UnboundLocalError local variable 'depth' refer

In summary, the conversation discusses a code for iterative deepening depth-first search in Python. It involves using a Python dictionary as an adjacency list and implementing a depth-limited search algorithm. The code also includes a function to check for the goal and a main function to run the search. There are some coding and naming issues mentioned, such as the need for a proper variable for the depth and better naming conventions.
  • #1
shivajikobardan
674
54
Homework Statement
iterative deepening depth first search
Relevant Equations
code given below
This code is for iterative deepening depth first search in python.

Python:
# Python dictionary to act as an adjacency list
graph = {
  '7' : ['19','21', '14'],
  '19': ['1', '12', '31'],
  '21': [],
  '14': ['23', '6'],
  '1' : [],
  '12': [],
  '31': [],
  '23': [],
  '6' : []
}
visited=[]
goal='31'
depth=0

depth_limit=2
def dls(visited, graph, node,depth_limit):
    if(node==goal):
            print("goal found")
            return True

   
    if(depth>=0):
        #to print path
        if node not in visited:
            visited.append(node)
       
       

        for neighbor in graph[node]:
            dls(visited, graph, neighbor,depth_limit-1)
       

    return Falsedef iddfs(visited,graph,node):
    while True:
        solution=dls(visited,graph,node,depth_limit)
        if(solution==goal):
            print("Success goal find at depth=",depth)
            print("Path=",visited)    
        depth=depth+1
 
       

print("Following is the Depth-First Search")
iddfs(visited, graph, '7')

There are various pseudocodes available for this problem. They are as follows-:
I did my best to understand and implement the code but I seem to have failed. And it is getting really confusing. Can you help me?

Jn8SKnn_svfRWVe5N267NeoO_rHZrMBa8OA4MlTYtDqZVLmDZv.png

KQAsy146aRIyaCGW-ofUtvwByjkUyGstnj9x0QJBITXyNRQdh6.png


efN0uhp8K5fzG7r3cHj6gV-BnzwOt2r5OW22AIFUZ36lSvy_qd.png

S6IF2aIaR_cQMWJq6SbVVd7bJDDYyRTLslBMzYF5gxe5z6qAMQ.png

DoWpfL27TC35nCGUfxjZ73Tu_Gm6iqHcx15JgTJFPyNvlWtXue.png

eaIIpoKpCw0MO2YoN74quEG3nXGSGLA7KZuumSZSQ8pNxKxuXi.png

ssO3bK2FRpqNn_WAijet0HUZk2--YjO_5wHegsHA5WGl7NNGVG.png

60hNRyR6hIxqtpPBnyzP009UWOKfH80-XWFPx37hKoVry0Xbb-.png

uQBXjIkKCMtEbjlxk8_jYDwfdvfgsfYOVVP88o-9FqM5mXkuqn.png

IcGM4EoHitCnm_4NzygFWTE4RDXixnUZcLLzjy00FSExNjylzl.png

mb7KsgHTDtebTxSmSzJWyQWI3IVBpZuZ1q6u7XPTMpMGqILJGd.png

L-LMt0D_HfXM1m_tJlzL8RumLcoEhUnbd-OH4UtwxVHaSbuBeU.png

VfFqCgCoSbnNtfco7zU-8d-2wQquG_L_UE4A8G7wg0DIYq57i9.png

HONDFVp9-IJS6iAWQzDJjXJu4hh20Le53nAsSizUnxll92KbB8.png

z-sLDPD7CT8p44vgkjTs-hDnfupFfq10yGVXVRII89jcEndWZ1.png
 
Last edited by a moderator:
Physics news on Phys.org
  • #2
  1. Post Python code with the starting tag CODE=Python
  2. In line 17 you have
    Python:
    def dls(visited, graph, node,depth_limit):
    Apart from the inconsistent spacing and the obscure name, do you not think that the value you are searching for should be an argument of the function?
  3. In lines 40-44 you are getting confused between depth, which you never initialise, and the global variable depth_limit, which I suggest you rename initial_depth_limit.
 
  • Like
Likes berkeman, shivajikobardan and jack action
  • #3
pbuk said:
Post Python code with the starting tag CODE=Python
I edited the OP to add the Python tag value. Thanks
 

FAQ: Exception has occurred: UnboundLocalError local variable 'depth' refer

What does "Exception has occurred: UnboundLocalError local variable 'depth' refer" mean?

This error means that there is a problem with a local variable called 'depth' in your code. The variable may not have been defined or it is not accessible within the current scope.

How do I fix an "UnboundLocalError" in my code?

To fix this error, you need to make sure that the local variable 'depth' is properly defined and accessible within the current scope. You may need to declare the variable before using it or check the indentation of your code.

Why am I getting an "UnboundLocalError" when my code was working before?

This error can occur if you have made changes to your code that have affected the scope of the local variable 'depth'. Make sure that the variable is still accessible in the current scope and that it is properly defined.

Can an "UnboundLocalError" be caused by a typo?

Yes, a typo in the name of the local variable 'depth' can cause this error. Make sure that you are using the correct variable name throughout your code.

Is there a way to prevent "UnboundLocalError" from occurring?

To prevent this error, make sure to properly define and declare all local variables before using them. Also, be aware of the scope of your variables and make sure they are accessible within the current scope.

Similar threads

Replies
3
Views
999
Replies
4
Views
1K
Replies
5
Views
2K
Replies
7
Views
2K
Replies
6
Views
3K
Replies
7
Views
2K
Back
Top