The 365 Data Science team is proud to invite you to our own community forum. A very well built system to support your queries, questions and give the chance to show your knowledge and help others in their path of becoming Data Science specialists.
Ask
Anybody can ask a question
Answer
Anybody can answer
Vote
The best answers are voted up and moderated by our team

Variable enclosing scope error

Variable enclosing scope error

0
Votes
0
Answer

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

No answers so far.
×
Learn Data Science
this Summer!
Get 50% OFF