Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 对Ceasar密码进行环绕_Python - Fatal编程技术网

Python 对Ceasar密码进行环绕

Python 对Ceasar密码进行环绕,python,Python,我想把我的Ceasar密码包起来。不幸的是,我不知道如何去实施它。以下是我目前的代码: 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_coun

我想把我的Ceasar密码包起来。不幸的是,我不知道如何去实施它。以下是我目前的代码:

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. 

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

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

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

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

你想知道如何让它跳过非字母字符吗?您能发布一些输入、当前输出和所需输出吗?所以您想知道如何使其跳过非字母字符?你能发布一些输入、电流输出和期望的输出吗?谢谢。我的输出更具可读性。但是,如果输出应打印“y”,则打印“-”。我的意思是,胆小鬼在死之前会死很多次;勇者只尝过一次死亡的滋味。在我所听到的所有奇迹中,人们害怕似乎是最奇怪的;看到死亡,一个必要的结局,当它来临的时候,它会到来。谢谢你,它现在工作得好多了!非常感谢。我的输出更具可读性。但是,如果输出应打印“y”,则打印“-”。我的意思是,胆小鬼在死之前会死很多次;勇者只尝过一次死亡的滋味。在我所听到的所有奇迹中,人们害怕似乎是最奇怪的;看到死亡,一个必要的结局,当它来临的时候,它会到来。谢谢你,它现在工作得好多了!