如果将变量“key”返回到解密函数中,为什么无法识别?错误显示“NameError:未定义全局名称'key'”这是代码:
alphabet = 'abcdefghijklmnopqrstuvwxyz'
alphaList = list(alphabet)
text = "g fmnc wms bgblr rpylqjyrc gr zw fylb. rfyrq ufyr amknsrcpq ypc dmp. bmgle gr gl zw fylb gq glcddgagclr ylb rfyr'q ufw rfgq rcvr gq qm jmle. sqgle qrpgle.kyicrpylq() gq pcamkkclbcb. lmu ynnjw ml rfc spj"
def createKey(aList):
key = []
count = 0
for letter in aList:
if count < 24:
key.append(aList[count + 2])
else:
key.append(aList[count - 24])
count += 1
return key
def decrypt(text):
createKey(alphaList)
decryptedList = []
textList = list(text)
for c in textList:
if c in alphaList:
index = alphaList.index(c)
decryptedList.append(key[index])
else:
decryptedList.append(c)
decryptedText = "".join(decryptedList)
return decryptedText
print decrypt(text)