I've set up an accumulator algorithm, but I initialized the accumulator variable outside of the block of code (in this example: main() ) that contains the accumulator loop:
alphabet = ""
def main():
    # 97 - 122 are the ASCII codes for the lower case alphabet
    for i in range( 97, 123 ):
        alphabet += chr( i )
    print alphabet
main()
For some reason I get this:
UnboundLocalError: local variable 'alphabet' referenced before assignment
The Solution I found was to initialize the variable inside the same block as the loop.
def main():
    alphabet = ""
    # 97 - 122 are the ASCII codes for the lower case alphabet
    for i in range( 97, 123 ):
        alphabet += chr( i )
    print alphabet
main()
My Question is why is this necessary?
Am I right in generalizing the problem and assuming that I need to initialize the variable in the same block as the loop, or is my problem specific to this example?
Is there some trait of python that I'm missing?
 
     
    