'dict' object has no attribute 'has_key'

后端 未结 6 984
太阳男子
太阳男子 2021-01-30 03:46

While traversing a graph in Python, a I\'m receiving this error:

\'dict\' object has no attribute \'has_key\'

Here is my code:

6条回答
  •  爱一瞬间的悲伤
    2021-01-30 04:25

    The whole code in the document will be:

    graph = {'A': ['B', 'C'],
                 'B': ['C', 'D'],
                 'C': ['D'],
                 'D': ['C'],
                 'E': ['F'],
                 'F': ['C']}
    def find_path(graph, start, end, path=[]):
            path = path + [start]
            if start == end:
                return path
            if start not in graph:
                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
    

    After writing it, save the document and press F 5

    After that, the code you will run in the Python IDLE shell will be:

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

    The answer you should receive in IDLE is

    ['A', 'B', 'C', 'D'] 
    

提交回复
热议问题