繁体   English   中英

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

[英]Adding a new list to a pre-defined list

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

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)

这样编辑怎么样?

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']]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM