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

时间:2019-02-27 00:08:12

标签: python python-3.x list encryption caesar-cipher

我有一个预定义列表,其中两个列表写为:

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

然后我有一个用于加密的Caeser密码,写为:

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)

1 个答案:

答案 0 :(得分: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']]