我遇到了一个奇怪的问题。此代码返回 None 而不是 True,即使它进入正确的分支并计算为 True:
edges = { (1, 'a') : [2, 3],
(2, 'a') : [2],
(3, 'b') : [4, 3],
(4, 'c') : [5] }
accepting = [2, 5]
loc = []
def nfsmsim(string, current, edges, accepting):
if string != "":
if ((current, string[0]) in edges.keys()):
global loc
loc = edges[(current, string[0])]
print "edge found:",loc
if (string == ""):
print "string is over",current,accepting
print type(current), type(accepting)
if current in accepting :
print "1"
return True
else:
print "2"
return 2
# fill in your code here
elif (current, string[0]) in edges.keys():
global loc
string = string[1:]
nfsmsim(string, loc[0], edges, accepting)
elif len(loc)>1:
global loc
nfsmsim(string, loc[1], edges, accepting)
# This problem includes some test cases to help you tell if you are on
# the right track. You may want to make your own additional tests as well.
print nfsmsim("abc", 1, edges, accepting)
这个的输出是:
string is over 5 [2, 5]
<type 'int'> <type 'list'>
1
None (<< instead of True)