簡體   English   中英

遞歸函數-無錯誤,無輸出

[英]Recursive function - No errors and no output

我是一個嘗試使用python的初學者。 我正在使用的代碼來自http://www.python.org/doc/essays/graphs/ 。目標是使用字典和遞歸函數獲得兩個節點之間的路徑。 運行它時,沒有任何輸出或任何錯誤。 我主要是在尋找某種可能導致這種情況的指針。

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')    

運行路徑查找器后,您需要以某種方式輸出結果。 一種方法是僅使用Python的內置print功能 ,該功能會將您的path輸出到標准輸出(您的終端或控制台)。 例如

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM