Python 如何修复索引器?
当我运行这个程序时,它应该在Caesar密码中对内容进行编码和解码,并选择解码选项,我得到的错误是字符串索引超出范围。有人能告诉我如何解决这个问题,并告诉我为什么会发生这种情况吗?我输入的要解码的文本是ibmmp,密钥是1。谢谢Python 如何修复索引器?,python,python-3.x,Python,Python 3.x,当我运行这个程序时,它应该在Caesar密码中对内容进行编码和解码,并选择解码选项,我得到的错误是字符串索引超出范围。有人能告诉我如何解决这个问题,并告诉我为什么会发生这种情况吗?我输入的要解码的文本是ibmmp,密钥是1。谢谢 alphabet = "abcdefghijklmnopqrstuvwxyz" encdec = input("Would you like to encode or decode a message? ") if encdec == "decode": key
alphabet = "abcdefghijklmnopqrstuvwxyz"
encdec = input("Would you like to encode or decode a message? ")
if encdec == "decode":
keyyn = input("Do you know the key to your encoded text? (Y/N) ")
if keyyn == "Y":
plaintext = input("Please type in your text ")
text = plaintext
key = int(input("What is the key? "))
for i in range(len(plaintext)):
letter = plaintext[i]
alphletter = alphabet.find(letter)
alphletter = alphletter - key
if alphletter < 0 or alphletter == 0:
alphletter = alphletter + 26
letter = alphabet[alphletter]
plaintext = plaintext + letter
else:
letter = alphabet[alphletter]
plaintext = plaintext + letter
print(plaintext.strip(text))
else:
print("This program is unable to decode a message without the key")
问题:ibmmp和1的键
我工作,b给你一个错误。原因如下:
如果alphletter<0,也可以使用:-这将不会处理多次缠绕f.e.210的键或负键-22
一些优化
# create a mapping dictionary so we do not need index()
alph = "abcdefghijklmnopqrstuvwxyz"
len_alph = len(alph)
d = {c:i for i,c in enumerate(alph)} # mapping char to index
d.update( {v:k for k,v in d.items()} ) # mapping index to char
d.update( {c:i for i,c in enumerate(alph.upper())} ) # mapping CHAR to index
def encode(text,key):
indexes = [d.get(c,"?") for c in text] # only mapped things, others get ?
# if ? use ? else lookup the correct replacement using % to make the index
# wrap around if above/below the index into alph
return ''.join(d.get((i+key)%len_alph if i != "?" else "?","?") for i in indexes)
def decode(text,key):
return encode(text,-key)
print(encode("tataaaa",5))
输出:
yfyffff
是否有可能给出一个导致此问题的示例?您需要显示完整的回溯。有很多地方可能发生这种情况,回溯会告诉我们发生了什么。欢迎来到StackOverflow。请按照您创建此帐户时的建议,阅读并遵循帮助文档中的发布指南。适用于这里。在您发布MCVE代码并准确描述问题之前,我们无法有效地帮助您。我们应该能够将您发布的代码粘贴到文本文件中,并重现您描述的问题。在需要输入的情况下,不进行调试尝试就将整个程序转储到我们身上是不可接受的。plaintext=inputPlease键入您的文本,keyyn=inputDo不会缩进到您的if语句下面。看起来存在一些缩进问题,尤其是if语句。修复这些和任何其他问题都会对我们有所帮助。谢谢你的帮助!这个程序现在可以运行了。我会使用优化,但是,这是我们目前正在研究的主题的一部分,即字符串操作,因此我必须这样做,因为我们还没有学会优化脚本中提到的所有内容。@WMurphy您最终会来的:很高兴它有帮助。
# create a mapping dictionary so we do not need index()
alph = "abcdefghijklmnopqrstuvwxyz"
len_alph = len(alph)
d = {c:i for i,c in enumerate(alph)} # mapping char to index
d.update( {v:k for k,v in d.items()} ) # mapping index to char
d.update( {c:i for i,c in enumerate(alph.upper())} ) # mapping CHAR to index
def encode(text,key):
indexes = [d.get(c,"?") for c in text] # only mapped things, others get ?
# if ? use ? else lookup the correct replacement using % to make the index
# wrap around if above/below the index into alph
return ''.join(d.get((i+key)%len_alph if i != "?" else "?","?") for i in indexes)
def decode(text,key):
return encode(text,-key)
print(encode("tataaaa",5))
yfyffff