return语句返回None而不是value

时间:2012-04-23 04:29:22

标签: python procedures

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

2 个答案:

答案 0 :(得分:8)

这是一个递归函数。当您到达终端案例(string == "")时,您会返回12。这将返回到调用函数 - 前一次调用nfsmsim。但是 nfsmsim调用并没有返回任何内容!您需要从nfsmsim的终端呼叫中获取值,并通过再次返回来传递它。

换句话说,您需要在if语句的这两个分支中的每个分支中使用return语句:

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)

答案 1 :(得分:1)

当函数结束时不使用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