0

Can Anybody tell me why I'm only getting one character of my plain text message to encrypt? The message is "the ships sail at midnight" and the encryption key is 4. I'm only able to get the t to shift to an x, the rest of the message does not print. What am I missing?

#request the message from the user
def InputMessage():
    PlainText = input("Enter the message you would like to encrypt: ")
    return PlainText

#encrypt the message
def CaesarShift(PlainText):

    #initialize variables
    alpha = ['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']
    Key = int(input("Enter the encryption key: "))
    CipherText = ""

    #skip over spaces in the message
    for ch in PlainText:
        if ch == " ":
            pass
        else:

            #encrypt the message 
            index = alpha.index(ch)
            NewIndex = index + Key
            ShiftedCharacter = alpha[NewIndex]
            CipherText += ShiftedCharacter
        return CipherText


#main program start
def main():
    PlainText = InputMessage()
    CipherText = CaesarShift(PlainText)

    #print the encrypted message
    print("Encrypted message: " + CipherText)

#main program end
main()
Mike Silva
  • 1
  • 1
  • 1
  • 5

1 Answers1

0

Your return statement is inside the loop, so the function returns after only enciphering the first letter.

You need to ensure that the indentation level of the return statement is the same as the indentation of the for loop.

Henry
  • 362
  • 2
  • 8