2017-03-19 3 views
-1

インデントに問題があります。誰かが私を助けてくれますか?エラーは1番目と2番目のif文にあります。python:インデントエラー:インデントが外側インデントレベルと一致しません

data_graph = {"a" : ["b", "d", "f"],"b" : ["c", "f"],"c" : ["d"],"d" : ["b"],"e" : ["d", "f"],"f" : ["d"] 
} 

def depth_first_search(data_graph): 
def depth_first(nodes_visited, data_graph, nodes): 

    if nodes in nodes_visited: 
     pass 
    else: 
     nodes_visited[len(nodes_visited):] =[nodes] 
     print ("Nodes visited:", nodes, len(nodes_visited)) 

    for i in data_graph[nodes]: 
     if i in nodes_visited: 
      pass 
     else: 
      depth_first(nodes_visited,data_graph, i) 

nodes_visited = [] 
while (len(data_graph) > len(nodes_visited)): 
    for nodes in data_graph: 
     if nodes in nodes_visited: 
      pass 
     else: 
      depth_first(nodes_visited, data_graph, nodes) 

depth_first_search(data_graph) 
+0

あなたの 'def depth_first_search(data_graph):'では何が起こっていますか?すぐ下に定義しようとする別の機能があります。あなたの意図が 'depth_first_search'の中に' depth_first'関数を持つことであるならば、 'depth_first_search'の下の全ては、その関数の中でインデントされる必要があります。 – idjaw

答えて

1

入れ子関数はインデントされていないことに注意してください。

data_graph = {"a" : ["b", "d", "f"],"b" : ["c", "f"],"c" : ["d"],"d" : ["b"],"e" : ["d", "f"],"f" : ["d"] 
} 

# you need to indent this function inside a function 
def depth_first_search(data_graph): 
    def depth_first(nodes_visited, data_graph, nodes): 

     if nodes in nodes_visited: 
      pass 
     else: 
      nodes_visited[len(nodes_visited):] =[nodes] 
      print ("Nodes visited:", nodes, len(nodes_visited)) 

     for i in data_graph[nodes]: 
      if i in nodes_visited: 
       pass 
      else: 
       depth_first(nodes_visited,data_graph, i) 

    nodes_visited = [] 
    while (len(data_graph) > len(nodes_visited)): 
     for nodes in data_graph: 
      if nodes in nodes_visited: 
       pass 
      else: 
       depth_first(nodes_visited, data_graph, nodes) 

    depth_first_search(data_graph) 
関連する問題