您可以在Python可視化工具中查看代碼。 在第18行306步,程序似乎跳過了elif語句,我真的找不到原因。Python在沒有任何理由的情況下會跳過某些循環迭代中的elif語句
關於函數:它需要一個特殊格式的列表,並使html和css標籤不在其中,以便爲我生成的HTML表格生成元素,打印的輸出不像它跳過的事實那麼重要這是哇的elif
這是我的代碼有一個內置的測試案例:
def HTML_lister(tbl):
'''Exports a list oflists with all the divs and css data is this format:[[][divTagStr, dayOfTheWeek, startTime, numberOfSlots, Css class key made of 'session CourseCode DayandTimeOfsession' ][]]'''
divList = []
for day in tbl:
session = []#each day starts with a new session
dayOftheWeek=day[0]
for i in range(1, len(day)):
slot = day[i]
if slot != None:
if len(session) == 0:
slotStr = time[i-1]
session.append(slotStr)
sessionName = slot[-11:]
session.append(sessionName)
session.append(slot[:-11])
session.append("30Min")
elif slot[-11:] != session[1][-11:]:#div creation here, then empty the session list
divEx= ['''<div class="session '''+ dayOftheWeek+slotStr.replace(".","Z")+''' " '''+'''
<h3>'''+
session[1]+" "+session[2]+ " "+session[0].replace(".",":")+"0"+'''
</h3>
</div>
''', dayOftheWeek,session[0],session.count("30Min")," "+"."+dayOftheWeek+slotStr.replace(".","Z")]
divList.append(divEx)
session= []
slotStr = time[i-1]
session.append(slotStr)
sessionName = slot[-11:]
session.append(sessionName)
session.append(slot[:-11])
session.append("30Min")
else:
session.append("30Min")
elif slot == None and len(session) != 0:
divEx= ['''<div class="session '''+ dayOftheWeek+slotStr.replace(".","Z")+''' " '''+'''
<h3>'''+
session[1]+" "+session[2]+ " "+session[0].replace(".",":")+"0"+'''
</h3>
</div>
''', dayOftheWeek,session[0],session.count("30Min")," "+"."+dayOftheWeek+slotStr.replace(".","Z")]
divList.append(divEx)
session = []
if len(session) != 0:
divEx= ['''<div class="session '''+ dayOftheWeek+slotStr.replace(".","Z")+''' " '''+'''
<h3>'''+
session[1]+" "+session[2]+ " "+session[0].replace(".",":")+"0"+'''
</h3>
</div>
''', dayOftheWeek,session[0],session.count("30Min")," "+"."+dayOftheWeek+slotStr.replace(".","Z")]
divList.append(divEx)
return divList
time=['8.0', '8.3', '9.0', '9.3', '10.0', '10.3', '11.0', '11.3', '12.0', '12.3', '13.0', '13.3', '14.0', '14.3', '15.0', '15.3', '16.0', '16.3', '17.0', '17.3', '18.0', '18.3', '19.0', '19.3', '20.0', '20.3', '21.0', '21.3', '22.0']
tbl = [['Sat', None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None], ['Sun', None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None], ['Mon', None, None, None, None, None, None, None, 'LEC MNT 202 MAT2322 A00', 'LEC MNT 202 MAT2322 A00', 'LEC MNT 202 MAT2322 A00', 'LEC LPR 155 ELG2138 A00', 'LEC LPR 155 ELG2138 A00', 'LEC LPR 155 ELG2138 A00', None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None], ['Tue', None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None], ['Wed', None, None, None, None, None, None, None, 'LEC LPR 155 ELG2138 A00', 'LEC LPR 155 ELG2138 A00', 'LEC LPR 155 ELG2138 A00', None, None, None, 'LAB CBY B302 ELG2138 A01', 'LAB CBY B302 ELG2138 A01', 'LAB CBY B302 ELG2138 A01', 'LAB CBY B302 ELG2138 A01', 'LAB CBY B302 ELG2138 A01', 'LAB CBY B302 ELG2138 A01', None, None, None, None, None, None, None, None, None, None], ['Thu', None, None, None, None, None, None, None, None, None, None, 'LEC MNT 202 MAT2322 A00', 'LEC MNT 202 MAT2322 A00', 'LEC MNT 202 MAT2322 A00', 'DGD MRT 221 ELG2138 A03', 'DGD MRT 221 ELG2138 A03', 'DGD MRT 221 ELG2138 A03', None, None, None, None, None, None, None, None, None, None, None, None, None], ['Fri', None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None]]
print(HTML_lister(tbl))
你有沒有人工測試過這一行? 'slot [-11:]!= session [1] [ - 11:]'。這是相當深的比較運行,你可以通過像unicode vs str(例如u「...」==「...」)或不同的回車或額外的空白那樣簡單。就我個人而言,我會通過使用[difflib.context_diff](https://docs.python.org/2/library/difflib.html#difflib.context_diff)之類的東西來審計該事件,以查看發生了什麼 – asdf
如何打印正確的條件在'if'之前查看您正在測試的內容。這是很多代碼,需要處理很長的列表(而且我不打算弄清楚元素-11應該是什麼)。這可以歸結爲易於測試的東西嗎? – tdelaney
正確縮進您的代碼。如果len(session)!= 0:'是循環的一部分還是不循環? – DyZ