2012-04-23 118 views
1

我遇到了一个奇怪的问题。此代码返回None,而不是真正的,即使它进入到正确的分支和评估为真:返回语句返回None而不是值

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) 
+6

你应该包括你的'的功能 – jamylak 2012-04-23 04:30:10

+3

def'那么我们就知道你应该做的是打印/返回! – Colleen 2012-04-23 04:32:14

+1

那里。更新了问题。代码中只有return语句 - 事实是我错过了这个问题。 – fixxxer 2012-04-23 04:33:15

回答

7

这是一个递归函数。当您到达终端机箱(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

[编辑]我不想给硬件太多;) – 2012-04-23 04:44:02

+1

@ Thr4wn,谢谢:) fixxxer,不要居高临下,但如果你找出自己的回报表。 – senderle 2012-04-23 04:44:29

+0

感谢您的指针! :)我最终设置了一个标志,并在函数结尾处基于它返回True。但它看起来并不是最干净的方式。 – fixxxer 2012-04-23 05:28:54

1

函数结束时是一样的使用收益无不使用返回指令。

由于功能是递归的,并且您使用它的结果,你必须返回的每呼吁也是其体内的价值:

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) 

你应该忘记使用全球禄。只要通过参数传递它。这是无论如何参考:

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 
相关问题