【发布时间】:2017-10-06 23:36:51
【问题描述】:
我正在使用其他人的代码来制作密码求解器,但它给了我
typeerror: 'str' object does not support item assignment"
在
key[keyIndex] = cipherletter
有没有办法与错误保持相同的含义? :)
def decryptWithCipherletterMapping(ciphertext, letterMapping):
# Return a string of the ciphertext decrypted with the letter mapping,
# with any ambiguous decrypted letters replaced with an _ underscore.
# First create a simple sub key from the letterMapping mapping.
key = ['x'] * len(LETTERS)
for cipherletter in LETTERS:
if len(letterMapping[cipherletter]) == 1:
# If there's only one letter, add it to the key.
keyIndex = LETTERS.find(letterMapping[cipherletter][0])
key[keyIndex] = cipherletter
else:
ciphertext = ciphertext.replace(cipherletter.lower(), '_')
ciphertext = ciphertext.replace(cipherletter.upper(), '_')
key = ''.join(key)
# With the key we've created, decrypt the ciphertext.
return simpleSubCipher.decryptMessage(key, ciphertext)
【问题讨论】:
-
在 Python 中,字符串是不可变的,所以你不能就地改变它们的字符。
标签: python-3.x