def removeNonAlpha(myString, key):
from string import ascii_lowercase
alphabet = ascii_lowercase + ' '
myString = myString.lower()
cipherText = ' '
for ch in myString:
if ch == 1 or ch == 2 or ch == 3 or ch == 4 or ch == 5 or ch == 6 or ch == 7 or ch == 8 or ch == 9 :
idx = alphabet.find(ch)
cipherText = cipherText + key[idx]
return cipherText
Running my script with:
removeNonAlpha('My favorite Cat was named Patches', alphabet)
returns the following error
7 if ch == 1 or ch == 2 or ch == 3 or ch == 4 or ch == 5 or ch == 6 or ch == 7 or ch == 8 or ch == 9 :
8 idx = alphabet.find(idx)
----> 9 cipherText = cipherText + key[idx]
10 return cipherText
UnboundLocalError: local variable 'idx' referenced before assignment
What is wrong with my code?