我在缩进方面遇到了麻烦。有人可以帮我吗:错误在第一个和第二个if语句中
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 :(得分: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)