1

I have seen multiple threads with people having the same problem, but it seems solutions have been offered on a case-by-case basis due to the unique nature of the problem

Here's my code:

loga = [(912, "Message A1") , (1000, "Message A2") , (988, "Message A3") , (1012, "Message A4") , (1002, "Message A5")] logb = [(926, "Message B1") , (1008, "Message B2") , (996, "Message B3") , (1019, "Message B4") , (1100, "Message B5")] logc = [(1056,"Message C1") , (1033, "Message C2") , (999, "Message C3") , (1054, "Message C4") , (1086, "Message C5")] logs = [loga, logb, logc] out = [] def find_lowest_i(lst): for i in range(len(lst)): log = lst[i] if log: t = log[0][0] if i==0 or t < lowest_t: lowest_i = i lowest_t = t return lowest_i while True: i = find_lowest_i(logs) print "i=", i tpl = logs[i].pop(0) print tpl out.append(tpl) print out 

And my exact error:

"Message File Name Line Position Traceback 19 find_lowest_i 13
UnboundLocalError: local variable 'lowest_t' referenced before assignment"

1
  • Not really sure what trouble you're having... Commented Jun 22, 2012 at 1:42

1 Answer 1

1

Due to the continuous poping, one of the logs (with this data, the first log) will eventually be empty:

logs = [[], [(1019, 'Message B4'), (1100, 'Message B5')], [(1056, 'Message C1'), (1033, 'Message C2'), (999, 'Message C3'), (1054, 'Message C4'), (1086, 'Message C5')]] 

In this situation, if log will fail for i=0, and lowest_t is not initialized. On the next iteration, i=1, there is a log but no lowest_t. Exception!

Sign up to request clarification or add additional context in comments.

1 Comment

Ahh I see! This makes sense. It's probably obvious that I'm not very advanced with python. Would I use an exception?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.