您可以在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