So the below Python code gets the error: TypeError: 'NoneType' object has no attribute 'getitem'
I can't figure out why the list 'path1' doesn't get recognized as a list, but instead is recognized as NoneType.
I've checked previous Stack questions, googled, all of that, but I can't figure out why it is happening. This came close-ish (I think) but I can't tell why my state = path[-1] call is turning up this error.
Any thoughts? Much appreciated.
Thanks
Code:
import re
import string
gr = {
"A":["B","C","D"],
"B":["A","E"],
"C":["A","E"],
"D":["A","H","I","J"],
"E":["B","C","F","G","H"],
"F":["E"],
"G":["E","H"],
"H":["D","E","G","K"],
"I":["D","K","L"],
"J":["D","M"],
"K":["I","M","H"],
"L":["I"],
"M":["K","J"]
}
def graphSearch(graph, start, dest):
frontier = [[start]]
explored = []
options = []
if frontier == []: return "I hope you enjoyed riding aboard the failboat"
while len(frontier)>0:
path = frontier.pop()
state = path[-1]
if state == dest:
return path
else:
for a in graph[state]:
if a not in path:
newP = path.append(a)
frontier.append(newP)
return options
print graphSearch(gr, "A", "M")