Caesar Cipher program was working but now isn't(help please) - Python 3
Question: I'm writing a Caesar Cipher code for a part of a controlled assessment. I
built a fully functioning program and I thought I had it sussed but after
changing a few things around I went to check back and everything has gone
wrong!

The code's quite untidy but I'm getting a bit sick of coding this now and have
taken to the internet to get someone else's view.

Code:

    
    
    answer ="C"
    while answer == "C":
    
        lettersList=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
    
        def menu():
            userChoice=input("Would you like to encrypt or decrypt a message? E or D.\n").lower()
            while userChoice != "e" and userChoice != "d":
                print("Invalid.")
                userChoice=input("Would you like to encrypt or decrypt a message? E or D.\n").lower()
            print("\n")
            return userChoice
    
        def getPlaintext():
            plaintext= input("Please enter the message you would like encrypted/decrypted\n").lower()
            while plaintext.isalpha() == False:
                print("Invalid")
                plaintext=input("Please enter the message you would like encrypted/decrypted\n").lower()
            print("\n")
            return plaintext
    
        def getKey():
            key=int(input("Please enter a key. 1-26\n"))
            while key > 26 or key < 1:
                print("Invalid.")
                key=int(input("Please enter a key. 1-26\n"))
            print("\n")
            return key
    
    
        def encryptText(plaintext,key):
            characterNumber = 0
            newMessage = ""
            for characters in plaintext:
                character = plaintext[characterNumber]
                characterPosition = lettersList.index(character)
                newPosition=character+key
                newLetter = lettersList[newPosition]
                newMessage = (newMessage+newLetter)
                characterNumber= characterNumber+1
            print(newMessage)
    
        def decryptText(plaintext,key):
            characterNumber = 0
            newMessage = ""
            for characters in plaintext:
                character = plaintext[characterNumber]
                characterPosition = lettersList.index(character)
                print(characterPosition)
                newPosition=characterPosition-key
                newLetter = lettersList[newPosition]
                newMessage = (newMessage+newLetter)
                characterNumber= characterNumber+1
                newMessage = (newMessage.lower())
            print(newMessage)
    
        userChoice=menu()
        plaintext=getPlaintext()
        key=getKey()
        if userChoice == "e":
            encryptText(plaintext,key)
        elif userChoice == "d":
            decryptText(plaintext,key)
        print(newMessage)
    


Answer: In decypryptText, newPosition can be negative. You should wrap around the
whole alphabet in this case. The best way to do that is to use the % operator:

    
    
    newPosition = (characterPosition - key) % len(lettersList)
    

By the way, using this in encryptText too lets you make lettersList only one
copy of the alphabet, instead of the two copies you use.

There are other things that can be polished in this code, but they are not as
important as this one.

Edit: if you want it capitalized for decryptText, you can make decryptText
just return newMessage.upper(). In the same way, you can make encryptText
return newMessage.lower().

