Caesar's Cipher in Basic Python

Hi,

I wrote this program to use the Caeser Cypher method to encode input from the user. I have figured out how to encode each Character, but I cant seem to concatenate the encoded Characters back together and return an encoded string on a single line? Here is where I am at so far, Any help would be greatly appreciated. Thanks in advance.

print (“Please tell me the password you want encrypted:”)
message = input()
length = len(message)
index = 0
index = int(index)
while (index < length):
oneChar = message[index]
ascNumber = ord(oneChar)
ascNumber = ascNumber + 9
if ascNumber >= 122:
ascNumber = ascNumber - 26
ascChar = chr(ascNumber)
print ("Your Encrypted Password is: ", ascChar)
index = index + 1

I realize this question is over 13 years old, but I had to fiddle with this, and you were close!

print("Please tell me the password you want encrypted:")
message = input()
length = len(message)
index = 0
encoded_message = ""  # Empty string to store the encoded message
while index < length:
    oneChar = message[index]
    ascNumber = ord(oneChar)
    ascNumber = ascNumber + 9
    if ascNumber >= 122:
        ascNumber = ascNumber - 26
    ascChar = chr(ascNumber)
    encoded_message += ascChar  # This is the part that is missing
    message
    index = index + 1

print("Your Encrypted Password is:", encoded_message)  # Print the entire encoded message on a single line

To concatenate the encoded characters and return an encoded string on a single line, you need to accumulate the encoded characters in a variable, then print the entire accumulated string after the loop finishes. Here’s a corrected version of your program:

print("Please tell me the password you want encrypted:")
message = input()
length = len(message)
index = 0
encrypted_message = ""

while index < length:
    oneChar = message[index]
    ascNumber = ord(oneChar)
    ascNumber = ascNumber + 9
    if ascNumber >= 122:
        ascNumber = ascNumber - 26
    ascChar = chr(ascNumber)
    encrypted_message += ascChar  # Concatenate the encoded character
    index += 1

print("Your Encrypted Password is:", encrypted_message)

Explanation:

  1. Initialization : An empty string encrypted_message is used to store the concatenated encoded characters.

  2. While Loop : The loop iterates through each character in the input message.

  3. Character Encoding : Each character is encoded using the Caesar Cipher method.

  4. Concatenation : The encoded character ascChar is concatenated to encrypted_message using += .

  5. Print Result : After the loop, the entire encrypted message is printed in one line.

Example:

If the input message is “hello”, the program will output an encrypted password according to the Caesar Cipher with a shift of 9.