将新列表添加到预定义列表

问题描述 投票:0回答:1

我有一个预定义的列表,里面写着两个列表:

passwords = [["yahoo","XqffoZeo"],["google","CoIushujSetu"]]

然后我有一个加密的Caesar密码写成:

encryptionKey = 16
def passwordEncrypt (unencryptedMessage, encryptionKey):
    encryptedMessage = ''
        for symbol in unencryptedMessage:
            if symbol.isalpha():
                num = ord(symbol)
                num += encryptionKey

                if symbol.isupper():
                    if num > ord('Z'):
                        num -= 26
                    elif num < ord('A'):
                        num += 26
                elif symbol.islower():
                    if num > ord('z'):
                        num -= 26
                    elif num < ord('a'):
                        num += 26

                encryptedMessage += chr(num)
            else:
                encryptedMessage += symbol

       return encryptedMessage

我给用户一系列选择,其中一个选项提示用户输入新网站和他们想要的网站密码。我需要弄清楚如何使用passwordEncrypt()函数加密新密码,将新网站和新密码添加到新列表,然后将新列表添加到上面的“密码”列表中。这是我到目前为止:

if choice == '3':
    print("What website is this password for?")
    website = input()
    print("What is the password?")
    unencryptedPassword = input()

    encryptedPassword = passwordEncrypt(unencryptedPassword, encryptionKey)
python python-3.x list encryption caesar-cipher
1个回答
0
投票

这样编辑怎么样?

if choice == '3':
    print("What website is this password for?")
    website = input() #'test'
    print("What is the password?")
    unencryptedPassword = input() #'ABZZZA'

    encryptedPassword = passwordEncrypt(unencryptedPassword, encryptionKey)

    #adding the new list(website, password) to the passwords list
    passwords.append( [website, encryptedPassword] )
    print(passwords)

结果:

[['yahoo', 'XqffoZeo'], ['google', 'CoIushujSetu'], ['test', 'QRPPPQ']]
© www.soinside.com 2019 - 2024. All rights reserved.