Last answered:

21 Oct 2020

Posted on:

17 May 2020


Variable enclosing scope error

I'm receiving this error in my script "Local Variable 'variable name' defined in enclosing scope of line 14 reference before assignment" I'm trying to create Caesar Cipher by myself.   Why do we get this error and how it can be solved ? code I'm using is -------------------------------------------------------------------------------------------- '''Caesar Cipher''' letter = 'abcdefghijklmnopqrstuvwxyz'
old_word = []
old_position = []
new_word = []
new_position = []
def encrypt(word,number):

for char in range(len(word)):

#adding the letters to the old_word list


#finding and then adding the position of old_word to position list


#with the help of position list we add the number and shift the alphabet
# to make a new list with latest position

'''if the index of character + number is more than 25 then'''

if old_position[char] + number > 25:

new_position.append(old_position[char] - number)


'''if the index of character + number is more than 25 then'''

new_position = [nchar + number for nchar in old_position]

#adding the letters to new_word list with new positions


print(f'{new_word[char]}, end=' '') old_word.clear()
1 answers ( 0 marked as helpful)
Posted on:

21 Oct 2020

Hi Jeevanshu,  thanks for reaching out! Could you please provide a snapshot of your code along with the error message you're receiving. From what you've posted there seems to be an error on line 14, however, without a reference to your actual code, I am unable to find out which this line is refering to. It'd be really helpful if you could also provide a link to the lecture this question is related to.  Thanks in advance! Best,  The 365 Team

Submit an answer