2017-06-17 97 views
-2

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

你有没有人工测试过这一行? 'slot [-11:]!= session [1] [ - 11:]'。这是相当深的比较运行,你可以通过像unicode vs str(例如u“...”==“...”)或不同的回车或额外的空白那样简单。就我个人而言,我会通过使用[difflib.context_diff](https://docs.python.org/2/library/difflib.html#difflib.context_diff)之类的东西来审计该事件,以查看发生了什么 – asdf

+0

如何打印正确的条件在'if'之前查看您正在测试的内容。这是很多代码,需要处理很长的列表(而且我不打算弄清楚元素-11应该是什么)。这可以归结为易于测试的东西吗? – tdelaney

+0

正确缩进您的代码。如果len(session)!= 0:'是循环的一部分还是不循环? – DyZ

回答

-2

我看到一个相当有这几个问题,一,你的代码似乎没有缩进,我的意思是当你运行print(HTML_Lister(tbl))时,它将返回None,因为在你使用它时没有运行代码。我不确定这是否是您遇到的实际问题,因为您似乎没有提及它,因此它运行良好。另外,你在这里调用它而不是day[0],它应该是tbl[day]

而且我不确定这是否是问题,因为您没有提及if语句本身没有完全运行,但len(day)将等于1,因为day是迭代器,可能是因为它小于7似乎是表示一周的几天,所以for i in range(1, len(day))正在做什么,因为它是更典型的范围从0开始,len(day)的范围实际上是0,所以你说的是for i in range(1, 0),这显然没有做任何事情。注意:我测试了它,并且实现了len(day)实际上会引发TypeError,因为整数没有长度。

同样,一天不是一个清单,所以如果它是运行它,据我所知,它不会,slot = day[i]会调出TypeErrordayOftheWeek = day[0]也会如此。

但是,除此之外,您的代码跳过elif语句的原因是因为您在代码的开头执行session = [],并且这使得len(session)等于0,因为其中没有项目的列表没有长度。这导致它不执行else语句,因为if语句是真实的。

+0

我这样做:>>> session = [“11。3“,\t”MAT2322 A00“,\t”LEC MNT 202“\t,”30Min“,\t”30Min“\t”30Min“] >>> slot =”LEC LPR 155 ELG2138 A00“>>> slot [-11: ]!= session [1] [ - 11:] >>>然而,在大程序中并不起作用,即使我测试了这种特殊情况,在termianl中,它回调True,但在程序中它不会“T – Mohammed

相关问题