Search code examples
pythonpython-3.xlistencryptioncaesar-cipher

Adding a new list to a pre-defined list


I have a pre-defined list with two lists inside written as:

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

Then I have a Caeser Cipher for encryption written as:

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

I give the user a series of choices, one of which prompts the user to input a new website and the password they want for that website. I need to figure out how to encrypt the new password using the passwordEncrypt() function, add both the new website and the new password to a new list, then add that new list to the "passwords" list above. This is what I have so far:

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

    encryptedPassword = passwordEncrypt(unencryptedPassword, encryptionKey)

Solution

  • How about editing like this?

    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)
    

    Outcome:

    [['yahoo', 'XqffoZeo'], ['google', 'CoIushujSetu'], ['test', 'QRPPPQ']]