1

我遇到了一个奇怪的问题。此代码返回 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)
4

2 回答 2

9

这是一个递归函数。当您到达终端案例 ( string == "") 时,您返回12。这将返回给调用函数——上一次调用nfsmsim. 但是of调用nfsmsim并没有返回任何东西!您需要从终端调用中获取值nfsmsim并通过再次返回来传递它。

换句话说,您需要在语句的这两个分支中的每一个中都有一个 returnif语句:

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)
于 2012-04-23T04:38:19.117 回答
2

函数结束时不使用 return 命令与使用 return None 相同。

由于该函数是递归的并且您正在使用它的结果,因此您必须在其主体内返回每个调用的值:

elif (current, string[0]) in edges.keys():
    global loc
    string = string[1:]
    return nfsmsim(string, loc[0], edges, accepting)
elif len(loc)>1:
    global loc
    return nfsmsim(string, loc[1], edges, accepting)

您应该忘记使用全局 loc。只需通过参数传递它。无论如何,它是一个参考:

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, loc): 

    if string != "":
        if ((current, string[0]) in edges.keys()):
            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():
        string = string[1:]
        return nfsmsim(string, loc[0], edges, accepting, loc)
    elif len(loc)>1:
        return nfsmsim(string, loc[1], edges, accepting, loc)


# 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, loc)

它在我的控制台上打印以下内容:

c:\tmp\___python\fixxxer\so10274792>python a.py
edge found: [2, 3]
edge found: [4, 3]
edge found: [5]
string is over 5 [2, 5]
<type 'int'> <type 'list'>
1
True
于 2012-04-23T08:55:04.497 回答