在输入字符串中移动值

时间:2017-02-19 17:15:00

标签: python

我试图做一个Caesar Cipher而且我已经非常接近了。问题是我的输出只打印出一个字母,而不是整行加密线。 E是英语中最常用的字母,因此想法是找到E与输入中最常用的字母之间的距离。然后把所有东西都移开那个距离。我是python的新手,所以我还不是很好。非常感谢提前!这是我到目前为止的代码:

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)

1 个答案:

答案 0 :(得分:1)

你在caesar_cipher函数中错误地使用return语句。

将循环语句从循环中移出:

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