. Advertisement .
..3..
. Advertisement .
..4..
I don’t know what I’m doing wrong, but I’ve already lost a couple of days struggling with this. Here is my command line:
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
This returns:
dict' object has no attribute 'has_key'
I don’t have any experience with the “attributeerror: ‘dict’ object has no attribute ‘has_key’”. In this case, how should I change?
The cause:
The error is on the 5th line, you are using
has_key(key)
but Python 3.0 does not accept it.Solution:
To solve this error, you have to use
__contains__(key)
instead ofhas_key(key)
We also have checked it in python3.7:
Another solution for this error is using
in
as below:Python 3 removed
has_key
. From the documentationHere’s an example.