2015-06-15 70 views
0

其他條件未執行。我無法在else語句之後打印數據。 我得到輸出RESP = [],而不是RESP [0] = - 1代碼無法在python中的elif中達到else語句

爲什麼我收到這是存在在我的代碼::

考慮下面的代碼片段

任何錯誤
import os 
import re 
import time 
import sys 
import random 
res="Received CommandCEPTfrfg45g45g54g54hg \ 
Received ComTvtv56g56gh65ghbtg56hg3g5ty7u6y45g4 vReceived ComRCE_vrff \ 
    Rece mcmprepmoinReceivedCommanAPcdcdcdcReceived CommanP" 
def checkit(val): 
    resp=[] 
    test_comment=[] 
    if val=='0x01': 
     print "reached condition" 
     if re.search("Received Command fcvdfc:",res): 
      resp.append(0) 
    elif(val=='0x02'): 
    #if re.search("Received",res): 
     resp.append(0) 
    elif(val=='0x03'): 
     if re.search("Received Command wedwedewd:",res): 
      resp.append(0) 
    elif(val=='0x04'): 
     if re.search("Receive 1",res): 
      resp.append(0) 
    elif(val=='0x05'): 
     if re.search("Receiv2",res): 
      resp.append(0) 
    elif(val=='0x06'): 
     if re.search("Recei2",res): 
      resp.append(0) 
    elif(val=='0x07'): 
     if re.search("Received2 Co",res): 
      resp.append(0) 
    elif(val=='0x08'): 
     if re.search("Receiv3",res): 
      resp.append(0) 
    elif(val=='0x09'): 
     if re.search("Received7",res): 
      resp.append(0) 
    elif(val=='0x0a'): 
     print "reached condition" 
     if re.search("Received8",res): 
      resp.append(0) 
    elif(val=='0x0b'): 
     print "reached condition" 
     if re.search("Received8",res): 
      resp.append(0) 
    elif(val=='0x0c'): 
      print "reached condition" 
      if re.search("ReceivedT9",res): 
       resp.append(0) 
    elif(val=='0x0d'): 
     print "reached condition" 
     if re.search("fwefef",res): 
      resp.append(0) 
    else: 
     print "reached else condition" 
     resp.append(-1) 
     #return 
    test_comment.append(str(res)) 
    resp.append(test_comment) 
    #print test_comment 
    return resp 

li=['0x01','0x02','0x03','0x04','0x05','0x06','0x07','0x08','0x09','0x0a','0x0b','0x0c','0x0d']; 
for i in range(10): 
    val=random.choice(li) 
    print val 
    ret=checkit(val); 
    print "response:",ret,"\n" 
+0

它什麼時候會用'val'調用它不檢查? –

+0

你可以用'switch'而不是'if..elif'來代替Python嗎? – Virbhadrasinh

+0

@Virbhadrasinh?可能要仔細檢查... – jonrsharpe

回答

0
checkit("0x00") # makes the else branch executed 
+0

checkit(0)將只執行其他塊,但我需要爲True和False條件運行的代碼。 – Chandra

+0

將「0x00」添加到「li」列表中怎麼辦? – dlask

0

這是工作,如果我把其他條件後,如果條件,但最後我不知道爲什麼?

import os 
import re 
import time 
import sys 
import random 
res="Received CommandCEPTfrfg45g45g54g54hg \ 
Received ComTvtv56g56gh65ghbtg56hg3g5ty7uReceived Command wedwedewd6y45g4Received Command fcvdfc vReceived ComRCE_vrff \ 
Rece mcmprepmoinReceivedCommanAPcdcdcdcReceived CommanP" 
def checkit(val): 
    resp=[] 
    test_comment=[] 
if val=='0x01': 
    print "reached condition" 
    if re.search("Received Command fcvdfc:",res): 
     resp.append(0) 
    else: 
     print "reached else condition" 
     resp.append(-1) 
elif(val=='0x02'): 
#if re.search("Received",res): 
    resp.append(0) 
    else: 
     print "reached else condition" 
     resp.append(-1) 
elif(val=='0x03'): 
    if re.search("Received Command wedwedewd:",res): 
     resp.append(0) 
    else: 
     print "reached else condition" 
     resp.append(-1) 
elif(val=='0x04'): 
    if re.search("Receive 1",res): 
     resp.append(0) 
    else: 
     print "reached else condition" 
     resp.append(-1) 
elif(val=='0x05'): 
    if re.search("Receiv2",res): 
     resp.append(0) 
    else: 
     print "reached else condition" 
     resp.append(-1) 
elif(val=='0x06'): 
    if re.search("Recei2",res): 
     resp.append(0) 
    else: 
     print "reached else condition" 
     resp.append(-1) 
elif(val=='0x07'): 
    if re.search("Received2 Co",res): 
     resp.append(0) 
    else: 
     print "reached else condition" 
     resp.append(-1) 
elif(val=='0x08'): 
    if re.search("Receiv3",res): 
     resp.append(0) 
    else: 
     print "reached else condition" 
     resp.append(-1) 
elif(val=='0x09'): 
    if re.search("Received7",res): 
     resp.append(0) 
    else: 
     print "reached else condition" 
     resp.append(-1) 
elif(val=='0x0a'): 
    print "reached condition" 
    if re.search("Received8",res): 
     resp.append(0) 
    else: 
     print "reached else condition" 
     resp.append(-1) 
elif(val=='0x0b'): 
    print "reached condition" 
    if re.search("Received8",res): 
     resp.append(0) 
    else: 
     print "reached else condition" 
     resp.append(-1) 
elif(val=='0x0c'): 
     print "reached condition" 
     if re.search("ReceivedT9",res): 
      resp.append(0) 
     else: 
      print "reached else condition" 
      resp.append(-1) 
elif(val=='0x0d'): 
    print "reached condition" 
    if re.search("fwefef",res): 
     resp.append(0) 
    else: 
     print "reached else condition" 
     resp.append(-1) 
    #return 
test_comment.append(str(res)) 
resp.append(test_comment) 
#print test_comment 
return resp 

li=['0x01','0x02','0x03','0x04','0x05','0x06','0x07','0x08','0x09','0x0a','0x0b','0x0c','0x0d']; 
for i in range(10): 
    val=random.choice(li) 
    print val 
    ret=checkit(val); 
    print "response:",ret,"\n"