vendredi 16 juin 2017

Python would skips an elif statement in some loop iterations with no reason

You can view the code in Python visualizer. In line 18 step 306, the program seems to skip the elif statement, and I really couldn't find out why.

about the function: it takes a specially formatted list and makes html and css tags out of it to generate elements for an HTML table I'm making, the printed output doesn't matter as much as the fact that it's skipping the elif which is wow

This is my code with a built-in test case:

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))

Aucun commentaire:

Enregistrer un commentaire