2012-04-23 115 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 
相關問題