python:缩进错误:缩进与任何外部缩进级别都不匹配
我的压痕有问题。有人能帮我一下吗:第一个和第二个if语句中有错误python:缩进错误:缩进与任何外部缩进级别都不匹配,python,indentation,Python,Indentation,我的压痕有问题。有人能帮我一下吗:第一个和第二个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 node
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)
注意嵌套函数没有缩进
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)
您的
def depth\u first\u搜索(数据图):
?您正试图在下面直接定义另一个函数。如果您打算在深度优先搜索
中使用深度优先搜索
功能,则需要在该功能中缩进深度优先搜索
下的所有内容。