I have tried to understand this by looking in previous threads but I still don't understand why I get this error for only one of two variables in the following piece of code (the code sucks I know):
alfaphet=('abcdefghijklmnopqrstuvxyz')
cryptalfaphet=('defghjiklmnopqrstuvxyzabc')
spaceNumber=[]
textCopy=[]
def crypt():
    textCopy=[]
    print('print the text that you want to encrypt:')
    text=input()
    for i in range(len(text)):
        for j in range(len(alfaphet)):
            if text[i]==alfaphet[j]:
                textCopy.append(cryptalfaphet[j])
        if text[i]==' ':
        spaceNumber.append(i)
    for i in range(len(spaceNumber)):
        for j in range(len(text)):
            if list(range(len(text)))[j]==int(spaceNumber[i]):
                textCopy.insert(j, ' ')
    textCopy=''.join(textCopy)
    print(textCopy)
crypt()
This code works fine, but if I remove the
textCopy=[] 
asignment from the beginning of the def-block, I get an error like this:
Traceback (most recent call last):
File "C:/Python33/dekrypt.py", line 26, in <module>
crypt()
File "C:/Python33/dekrypt.py", line 13, in crypt
textCopy.append(cryptalfaphet[j])
UnboundLocalError: local variable 'textCopy' referenced before assignment
My question is why this doesn't happen with the spaceNumber variable. spaceNumber is as far I can see also referenced before asignment with the
spaceNumber.append(i)
asignment? It is referenced before the def-block, but so was the textCopy vaiable right? What is the difference, they're both empty lists from the beginning and I use the .append() method on both, but Python seems to treat them differently!?
 
     
    