Caesar Cipher Function in Python
When attempting to implement a Caesar cipher in Python, users may encounter an issue where only the last shifted character is displayed instead of the entire encrypted string.
The provided code shifts each individual character successfully but fails to concatenate these shifted characters into a new string. The following line in the code causes this issue:
cipherText = "" cipherText += finalLetter
To resolve this issue, the cipher text should be accumulated inside the loop:
cipherText = "" for ch in plainText: # ... cipherText += finalLetter
This ensures that all shifted characters are appended to the cipherText string, resulting in the correct encrypted output.
The above is the detailed content of Why does my Python Caesar cipher only display the last shifted character?. For more information, please follow other related articles on the PHP Chinese website!