在python中从列表中的列表追加

2024-09-28 23:39:33 发布

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

import sys
alphabet = ("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z")
rotor1 = ("p","l","m","k","o","n","j","i","b","h","u","v","g","y","c","f","t","x","d","r","z","s","e","a","w","q")
rotor2 = ("e","b","h","r","k","a","s","t","i","u","m","z","g","y","q","v","d","l","c","x","n","w","o","p","f","j")
rotor3 = ("test")
rotors = (alphabet,rotor1,rotor2,rotor3)
reflector = ("test")

def menu():
    print "Welcome to the Enigma machine!"
    print "------------------------------"
    print "1) Encrypt your message"
    print "2) Change the connections on the plugboard"
    print "3) Exit the program"
    userchoice = raw_input("Please choose an option")

    if userchoice == "1":
        encrypt()
    elif userchoice == "2":
        plugboard()
    elif userchoice == "3":
        sys.exit()

def encrypt():
    alphapos = []
    rotor1pos = []
    encryptedword = []
    userinput = raw_input("Please enter the message that needs to be encrypted")
    usermsglist = list(userinput)
    for x in range(0,2):
        for i in range(0,len(usermsglist)):
            alphapos.append(rotors[x].index(usermsglist[i]))
            encryptedword.append(rotor1[alphapos[i]])
            usermsglist = ''.join(encryptedword)
    print usermsglist

我正在尝试做一个for循环,使我的代码对于我的enigma机器更有效。但是当我从rotors列表中的一个项目追加时,我得到了一个错误,这个项目是列表字母表

我得到了错误

IndexError: string index out of range

这是导致错误的代码行,特别是转子[x]部分,但如果我将其更改为字母表,程序工作正常:

alphapos.append(rotors[x].index(usermsglist[i]))

Tags: theforindex错误sysrangeprintappend
2条回答

问题是你的索引管理。我将您的循环更改为使用enumerate,得到了一个结果,代码没有出错。你知道吗

import sys
alphabet = ("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z")
rotor1 = ("p","l","m","k","o","n","j","i","b","h","u","v","g","y","c","f","t","x","d","r","z","s","e","a","w","q")
rotor2 = ("e","b","h","r","k","a","s","t","i","u","m","z","g","y","q","v","d","l","c","x","n","w","o","p","f","j")
rotor3 = ("test")
rotors = (alphabet,rotor1,rotor2,rotor3)
reflector = ("test")

def menu():
    print "Welcome to the Enigma machine!"
    print "               "
    print "1) Encrypt your message"
    print "2) Change the connections on the plugboard"
    print "3) Exit the program"
    userchoice = raw_input("Please choose an option")

    if userchoice == "1":
        encrypt()
    elif userchoice == "2":
        plugboard()
    elif userchoice == "3":
        sys.exit()

def encrypt():
    alphapos = []
    rotor1pos = []
    encryptedword = []
    userinput = raw_input("Please enter the message that needs to be encrypted")
    usermsglist = list(userinput)
    for x in range(0,2):
        for i, s in enumerate(userinput):
            alphapos.append(rotors[x].index(s))
            encryptedword.append(rotor1[alphapos[i]])
            usermsglist = ''.join(encryptedword)
    print usermsglist

结果:

Please enter the message that needs to be encryptedhello
iovvciovvc


更新:

仔细查看代码后,它似乎失败了,因为您在第一次循环迭代结束时重新分配了usermsglist。你知道吗

循环检查的list突然改变。你知道吗

在第一次迭代中,usermsglist被分配给encryptedword,这是一个长度为1的列表。当循环将其值增加1时,它会查找usermsglist[1],但usermsglist等于['h'](在我的示例中),没有第1个元素,只有第0个

首先,您的错误来自您没有准备好rotor3。你知道吗

循环内的rotor[x + 1]导致此错误。你知道吗

下一步,有效的方法。。。不,这个程序似乎不起作用。。。。再试一次。。。你知道吗

提示:使用Python dictionaryord函数。你知道吗

相关问题 更多 >