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

时间:2014-07-07 20:58:26

标签: python python-2.7

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机器更有效。但是当我从转子列表中的项目追加列表字母

时,我收到错误

我收到错误

IndexError: string index out of range

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

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

2 个答案:

答案 0 :(得分:1)

问题在于您的索引管理。我改变了你的循环以使用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个< /强>

答案 1 :(得分:-2)

首先,您的错误来自您没有准备好rotor3

循环中的

rotor[x + 1]会导致此错误。

接下来,有效的方式......没有这个程序似乎不起作用....再试一次......

提示:使用Python dictionaryord函数。