Exception has occurred: UnboundLocalError local variable 'depth' refer

  • Context: Comp Sci 
  • Thread starter Thread starter shivajikobardan
  • Start date Start date
  • Tags Tags
    Depth Local Variable
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
2 replies · 2K views
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:
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.
 
Reply
  • Like
Likes   Reactions: 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