Ceasar Cipher的环绕声

时间:2017-02-20 22:29:18

标签: python

我正试图让我的Ceasar密码环绕。不幸的是,我不确定如何实施它。这是我目前的代码:

namespace bp = boost::python;

bp::object float_Type = bp::borrowed(
                            bp::downcast<PyTypeObject>(&PyFloat_Type))));

虽然我的代码可以将输入转换为可读的内容,但最终会删除过程中的空格和正确的标点符号。

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:
       if char.isalpha():
          cipher_process = ord(char)+shift_distance
          if cipher_process > ord('z'):
             cipher_process -= 26
           post_translation = chr(cipher_process)
           ciphered_text += post_translation 
     return ciphered_text 

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

1 个答案:

答案 0 :(得分:0)

你必须将它们转录成你的答案:

for char in unciphered_text:
    if char.isalpha():
        cipher_process = ord(char.upper()) + shift_distance
        if cipher_process > ord('Z'):
            cipher_process -= 26
        if cipher_process < ord('A'):  # Deal with underflow
            cipher_process += 26
        post_translation = chr(cipher_process)
        ciphered_text += post_translation
    else:  # skip, but include non-alphabetic characters
        ciphered_text += char

此外,您需要.upper()将char转换为大写,并处理A下面字符的下溢。

示例输出(事后添加了返回):

COWARDS DIE MANY TIMES BEFORE THEIR DEATHS; THE VALIANT NEVER TASTE 
OF DEATH BUT ONCE. OF ALL THE WONDERS THAT I YET HAVE HEARD, IT SEEMS 
TO ME MOST STRANGE THAT MEN SHOULD FEAR; SEEING THAT DEATH, A NECESSARY 
END, WILL COME WHEN IT WILL COME