簡體   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