Python 程序返回一个";索引器:字符串索引超出范围。”;

Python 程序返回一个";索引器:字符串索引超出范围。”;,python,string,Python,String,当我运行这段代码时,它返回一个“indexer-ror:String-index-out-range”错误。有人能帮我解决一下吗?我看不出有什么问题 from random import shuffle alphabet="abcdefghijklmnopqrstuvwxyz" def substitution(alphabet,plaintext): # Create array to use to randomise alphabet position

当我运行这段代码时,它返回一个
“indexer-ror:String-index-out-range”
错误。有人能帮我解决一下吗?我看不出有什么问题

from random import shuffle  

alphabet="abcdefghijklmnopqrstuvwxyz"  

def substitution(alphabet,plaintext):  

    # Create array to use to randomise alphabet position  
    randarray=range(0,len(alphabet))  
    shuffle(randarray)  

    key="Zebra"  

    #Create our substitution dictionary  
    dic={}  
    for i in range(0,len(alphabet)):  
        key+=alphabet[randarray[i]]  
        dic[alphabet[i]]=alphabet[randarray[i]]  

    #Convert each letter of plaintext to the corrsponding  
    #encrypted letter in our dictionary creating the cryptext  
    ciphertext=""  
    for l in plaintext:  
        if l in dic:  
            l=dic[l]  
        ciphertext+=l  
    for i in alphabet:  
        print i,  
    print  
    for i in key:  
        print i,  
    print  
    return ciphertext,key  

# This function decodes the ciphertext using the key and creating  
# the reverse of the dictionary created in substitution to retrieve  
# the plaintext again  
def decode(alphabet,ciphertext,key):  

    dic={}  
    for i in range(0,len(key)):  
        dic[key[i]]=alphabet[i]  

    plaintext=""  
    for l in ciphertext:  
        if l in dic:  
            l=dic[l]  
        plaintext+=l  

    return plaintext  

# Example useage  
plaintext="the cat sat on the mat"  
ciphertext,key=substitution(alphabet,plaintext)  
print "Key: ", key  
print "Plaintext:", plaintext  
print "Cipertext:", ciphertext  
print "Decoded  :", decode(alphabet,ciphertext,key)
回溯(最近一次呼叫最后一次): 文件“/Users/Devlin/Desktop/Dev/Python/Substitution Cypher.py”,第57行,in 打印“已解码:”,解码(字母表、密文、密钥) 文件“/Users/Devlin/Desktop/Dev/Python/Substitution Cypher.py”,第41行,在decode中 dic[key[i]]=字母表[i]索引器:字符串索引超出范围 问题在于:

def解码(字母、密文、密钥):
dic={}
对于范围(0,len(key))中的i:
dic[键[i]]=字母表[i]#此行失败
此时,您的
键始终为31个字符,即
len('Zebra')+len(字母表)
。由于
len(alphabet)
始终为26,因此
alphabet[i]
在i>25时失败

我相信您误解了这里的
键所表示的内容<代码>替换
应该生成一个随机密钥,它不是密码或任何类型的密码。事实上,如果你看一下,你会看到
替换中的
key=“
”,而不是一些随机值

Traceback (most recent call last): File"/Users/Devlin/Desktop/Dev/Python/Substitution Cypher.py", line 57, in print "Decoded :", decode(alphabet,ciphertext,key) File "/Users/Devlin/Desktop/Dev/Python/Substitution Cypher.py", line 41, in decode dic[key[i]]=alphabet[i] IndexError: string index out of range 这里,
len(键)==len(字母表)+5
。因此,
i
(迭代
范围(0,len(key))
)比字母表的实际长度更远。这是部分原因造成的

for i in range(0,len(key)):  
    dic[key[i]]=alphabet[i] 
因此,同样地,
key
中的字符比
alphabet
中的字符多,这导致了错误

解决办法是换线

key="Zebra"  #HERE, YOU START WITH ZEBRA

#Create our substitution dictionary  
dic={}  
for i in range(0,len(alphabet)):  
    key+=alphabet[randarray[i]]  #AND HERE, YOU ADD TO ZEBRA
    dic[alphabet[i]]=alphabet[randarray[i]]  

你为什么想让它变成“斑马”


*p.S
range(x)
range(0,x)
相同,因此您通常只需编写
range(len(key))
等*

发布准确的错误消息,包括行号和完整的回溯。
code
traceback(最近一次调用):文件“/Users/Devlin/Desktop/Dev/Python/Substitution Cypher.py”,第57行,打印“Decoded:”,解码(字母表、密文、密钥)文件“/Users/Devlin/Desktop/Python/Substitution Cypher.py”,第41行,在解码dic[key[i]=alphabet[i]索引器中:字符串索引超出范围
code
此外,如果您已经必须使用pastebin,则突出显示语法也很好…欢迎使用StackOverflow!如果您需要编辑问题的帮助,如格式化代码,请参阅
key="Zebra"
key=""