# This code is just for illustration, as it will not run because it's dependent # on numpy, copy, time and random. # Counter 'pop_i' was initially under the try-clause, after the function-call # 'msq.write_row(res_tmp[0], row_i=pop_i)'. Then it got increased even if the # function called raised an ValueError. By moving the 'pop_i' increase to # the finally clause, the expected result was produced. msq = MSquare(cl) res_bck = copy.deepcopy(res) res_tmp = [] pop_i = 0 while pop_i < cl: res_tmp.insert(0, res.pop(rnd.randint(0,len(res)-1))) work_ok = True try: msq.write_row(res_tmp[0], row_i=pop_i) # pop_i += 1 except: work_ok = False res.extend(res_tmp) res_tmp = [] msq.arr.fill(0) pop_i = 0 finally: if not work_ok: pass else: pop_i += 1