Last answered:

21 Oct 2020

Posted on:

17 May 2020

0

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

old_word.append(word[char])

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

old_position.append(letter.index(old_word[char]))

#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)

else:

'''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

new_word.append(letter[new_position[char]])

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

21 Oct 2020

0
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