2017-02-20 51 views
1

我正在尝试让我的塞萨尔密码回绕。不幸的是,我不知道如何去实施它。这里是我的代码,因为它是目前:塞萨尔密码绕回

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) 

虽然它最终消除在这个过程中的空间和适当的标点符号我的代码可以翻译输入到的东西可读。

Input: Frzdugv glh pdqb wlphv ehiruh wkhlu ghdwkv; Wkh ydoldqw qhyhu wdvwh ri ghdwk exw rqfh. Ri doo wkh zrqghuv wkdw L bhw kdyh khdug, Lw vhhpv wr ph prvw vwudqjh wkdw phq vkrxog ihdu; Vhhlqj wkdw ghdwk, d qhfhvvdub hqg, Zloo frph zkhq lw zloo frph 

Output: Cowardsdieman_timesbeforetheirdeathsThevaliantnevertasteofdeathbutonceOfallthewondersthatI_ethaveheardItseemstomemoststrangethatmenshouldfearSeeingthatdeathanecessar_endWillcomewhenitwillcome 

Desired Output: 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. 
+0

所以,你想知道如何使它跳过非字母字符?你能发布一些输入,电流输出和所需的输出吗? – TemporalWolf

回答

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()炭要转换为大写,并处理字符以下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 
+0

谢谢。我的输出更具可读性。但是,输出应该打印出'y',而是打印出“ - ”。这就是我的意思 – Amaranthus

+0

懦夫在死亡之前死于man_ times;勇士从来没有尝过死亡的味道,但曾经。在我所听到的所有奇迹中,我觉得最奇怪的是人们应该害怕;看到死亡,必要的结局,它会在什么时候到来 – Amaranthus

+0

非常感谢你现在工作得更好! – Amaranthus