2017-06-17 103 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

相關問題