Diez B. Roggisch wrote: > [EMAIL PROTECTED] schrieb: > >> okay, I try you suggestion, and re-write my code like this: >> colors = ["#ff0000", "#00FF00", "#0000FF"] >> colorIndex = 0 >> >> def getText(nodelist): >> >> >> for str in strings: >> >> print colors[colorIndex % colors.length] >> colorIndex += 1 >> >> but i get this error: >> print colors[colorIndex % colors.length] >> UnboundLocalError: local variable 'colorIndex' referenced before >> assignment > > > If I take your code, it works for me.
Probably because it never calls getText(). The problem is that colorIndex is being assigned inside getText(). This makes Python assume it is a local variable and it won't see the global colorIndex. The UnboundLocalError is telling you that there is no value (binding) for the local variable colorIndex. One solution is to move colorIndex into getText() and initialize it every time: def getText(nodelist): colorIndex = 0 for str in strings: print colors[colorIndex % colors.length] colorIndex += 1 If you want the value of colorIndex to persist so one call to getText() picks up where the last one left off, put a global statement inside getText() so Python knows colorIndex is global: def getText(nodelist): global colorIndex for str in strings: print colors[colorIndex % colors.length] colorIndex += 1 Kent -- http://mail.python.org/mailman/listinfo/python-list