如何修复索引器?

2024-10-02 06:27:23 发布

您现在位置:Python中文网/ 问答频道 /正文

当我运行这个程序(它应该对Caesar密码进行编码和解码)并选择decode选项时,我得到的错误是字符串索引超出了范围。有人能告诉我怎么解决这个问题,告诉我为什么会这样吗?我输入的要解码的文本是ibmmp,键是1。谢谢。你知道吗

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

Tags: orthetokeytextyouinputif
1条回答
网友
1楼 · 发布于 2024-10-02 06:27:23

问题:ibmmp和1的键

i起作用,b给您一个错误。原因如下:

alphletter = alphabet.find(letter)              #  ==> 1
alphletter = alphletter - key                   #  ==> 0
if alphletter < 0 or alphletter == 0:           #  ==> True
    alphletter = alphletter + 26                    #   ==> 26 
letter = alphabet[alphletter]                   #  only has indexes from 0 to 25
plaintext = plaintext + letter                  #   ~~~~ crash ~~~~
# (also: indentation error for the last 2 lines)

可以使用模运算符%修复溢出/下溢:

alphletter = (alphletter - key) % 26   # for -1 : 25

您也可以使用if alphletter < 0:-这将不会处理多次环绕的键(例如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

相关问题 更多 >

    热门问题