Comp Sci Exception has occurred: UnboundLocalError local variable 'depth' refer

AI Thread Summary
The discussion centers on a Python implementation of iterative deepening depth-first search (IDDFS) that encounters an UnboundLocalError due to the variable 'depth' not being properly initialized. Participants suggest that the depth variable should be passed as an argument to the recursive function to avoid confusion with the global depth_limit variable. There are also recommendations to improve code readability by renaming variables and addressing inconsistent spacing. The original poster expresses confusion over the implementation and seeks assistance in correcting the code. The conversation highlights the importance of variable scope and clarity in function definitions for effective coding.
shivajikobardan
Messages
637
Reaction score
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
  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
pbuk said:
Post Python code with the starting tag CODE=Python
I edited the OP to add the Python tag value. Thanks
 

Similar threads

Back
Top