如何在python中添加来自不同列表的列表项?

2024-10-05 14:28:21 发布

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

我正在尝试用python创建一个一次性pad-esque密码。为此,我将所有纯文本和键转换为ASCII码,然后将它们相加,然后将它们转换回常规字符。然而,当我尝试将这两个数字相加时,我却陷入了困境。这是我的密码:

#Creating initial variables
plainText = str(input('Input your plain text \n --> ')).upper()
key = str(input('Input your key. Make sure it is as long as your plain text.\n --> ')).upper()

#>Only allowing key if it is the same size as the plain text
if len(key) != len(plainText):
    print('Invalid key. Check key length.')
    key = str(input('Input your key. Make sure it is as long as your plain text. \n --> ')).upper()
else:
    plainAscii=[ord(i) for i in plainText]
    keyAscii=[ord(k) for k in key]

print (plainAscii)
print (keyAscii)

#Adding the values together and putting them into a new list
cipherText=[]
for i in range(0, len(key)):
    x = 1
    while x <= len(key):
        item = plainAscii[x] + keyAscii[x]
        cipherText.append(item)
        x = x + 1
print(cipherText)

我正在打印测试清单。但是,它仅在打印前两个列表后返回:

 Traceback (most recent call last):
  File "/Users/chuckii/Desktop/onetimepad.py", line 21, in <module>
    item = plainAscii[x] + keyAscii[x]
IndexError: list index out of range

请忽略我的少年用户名,我10岁的时候做的。提前谢谢。你知道吗


Tags: keytextininputyourlenasupper