Amaranthus
Amaranthus

Reputation: 105

Shifting Values in Input String

I'm trying to do a Caesar Cipher and I've come pretty close. The problem is my output only prints out a single letter, not the whole line ciphered line. E is the most frequently used letter in English so the idea is to find the distance between E and the most frequently used letter in the input. And then shift everything over by that distance. I'm new at python, so I'm not very good yet. Thanks so much in advance! Here's the code I have so far:

maximum_character = unciphered_text[0]
maximum_count = unciphered_text.count(unciphered_text[0])
for char in unciphered_text:
    if char is not " ":
        if unciphered_text.count(char) > maximum_count:
            maximum_character = char

print("The most frequent character used is: ", maximum_character) 

ASCII_maximum = maximum_character.lower()
ASCII_number = ord(ASCII_maximum)
print(ASCII_number)

shift_distance = ord('e')-ASCII_number
print("The shift distance is: ", shift_distance)

def caesar_cipher(unciphered_text, shift_distance):
    ciphered_text = ""
    for char in unciphered_text:
        cipher_process = ord(char)+shift_distance
        post_translation = chr(cipher_process)
        ciphered_text += post_translation 
        return ciphered_text 

answer = caesar_cipher(unciphered_text, shift_distance)
print(answer)

Upvotes: 0

Views: 68

Answers (1)

Pan Long
Pan Long

Reputation: 1032

You misindent return statement in caesar_cipher function.

Move your return statement out from loop:

def caesar_cipher(unciphered_text, shift_distance):
    ciphered_text = ""
    for char in unciphered_text:
        cipher_process = ord(char)+shift_distance
        post_translation = chr(cipher_process)
        ciphered_text += post_translation 
    return ciphered_text 

Upvotes: 1

Related Questions