Python function misbehaves when called multiple times -


i wrote function in python create simple 4 column table in html. when call file, returns table correctly.

issues arise, however, if called multiple times in single script. first 1 appears ought to. second time called, of rows beneath title row have 6 columns (two blank) instead of four. third time, there ten columns (six blank).

i started coding recently, don't know what's going on behind scenes here.

when function called twice or more times in succession, new instance of function called? variables 'reset' speak?

this code of called function:

def fourcolumntable(title1, list1, title2, list2, title3, list3, title4, list4):     error = 0     #check lists of same length     if(len(list1) != len(list2) or len(list1) != len(list3) or len(list1) != len(list4)):         error = 1         table = "error: lists must same length"      if(error == 0):         tablelist = []     #append <table> tag         tablelist.append('<table class="table table-bordered">')      #format list elements , titles         #put each title inside <th> tags         titlelist = []         titlelist.append(title1)         titlelist.append(title2)         titlelist.append(title3)         titlelist.append(title4)         in range(len(titlelist)):             titlelist[i] = "<th>" + str(titlelist[i]) + "</th>"          #put each string element inside <td> tags         in range(len(list1)):             list1[i] = "<td>" + str(list1[i]) + "</td>"         in range(len(list2)):             list2[i] = "<td>" + str(list2[i]) + "</td>"         in range(len(list3)):             list3[i] = "<td>" + str(list3[i]) + "</td>"         in range(len(list4)):             list4[i] = "<td>" + str(list4[i]) + "</td>"      #put list elements in tablelist         tablelist.append('<thead>')         in range(len(titlelist)):             tablelist.append(titlelist[i])         tablelist.append('</thead>')         tablelist.append('<tbody>')         in range(len(list1)):             tablelist.append('<tr>')             tablelist.append(list1[i])             tablelist.append(list2[i])             tablelist.append(list3[i])             tablelist.append(list4[i])             tablelist.append('</tr>')         tablelist.append('</tbody>')      #close <table> tag         tablelist.append('</table>')      #assign tablelist 1 variable         table = ''.join(tablelist)     return table 

i suspect (but not all) of listn arguments being reused between calls. leads bug, because code modifies provided lists each time called. adds <td> , </td> around each list item. if repeatedly on same list, items end multple nested tags, e.g. <td><td><td>...</td></td></td>.

this gets rendered empty columns, browser fills in missing closing tags between repeated <td> opening tags , ignores closing tags @ end.

a first quick fix create new list modified items, rather modifying provided list (here using list comprehension):

list1 = ["<td>" + item + "</td>" item in list1] 

a further improvement use library create table, rather creating string manipulation yourself. there variety of xml templating libraries use, don't have enough experience of them make strong suggestion. this page might place start browsing.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -