其他條件未執行。我無法在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"
它什麼時候會用'val'調用它不檢查? –
你可以用'switch'而不是'if..elif'來代替Python嗎? – Virbhadrasinh
@Virbhadrasinh?可能要仔細檢查... – jonrsharpe