Home  >  Article  >  Backend Development  >  Why does my Python Caesar cipher only display the last shifted character?

Why does my Python Caesar cipher only display the last shifted character?

Patricia Arquette
Patricia ArquetteOriginal
2024-10-31 15:53:02424browse

Why does my Python Caesar cipher only display the last shifted character?

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn