简体   繁体   中英

Recursive function - No errors and no output

I'm a beginner trying to use python. The code I'm working with is from http://www.python.org/doc/essays/graphs/ The goal is to obtain a path between two nodes using a dictionary and a recursive function. When I run it, I don't get any output or any errors. I'm mainly looking for somekind of pointer as to what could cause this.

def find_path(graph, start, end, path=[]):
    path = path + [start]
    if start == end:
        return path
    if not graph.has_key(start):
        return None
    for node in graph[start]:
        if node not in path:
            newpath = find_path(graph, node, end, path)
            if newpath: return newpath
    return None

graph = {'A': ['B','C'],'B': ['C','D'],'C': ['D'],'D': ['C'],'E': ['F'],'F':   ['C']}   
find_path(graph,'A','D')    

After running your path finder, you need to output the result somehow. One way is to just use Python's built-in print function which will output your path to standard out (your terminal or console). Eg

print find_path(graph, 'A', 'D')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM