2017-02-19 45 views
0

我正在嘗試做一個凱撒密碼,並且我已經非常接近了。問題是我的輸出只打印出單個字母,而不是整行的加密行。 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

您misindent在caesar_cipher功能return語句。

從環出移動你的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 
+0

它的工作!非常感謝!我不知道touchy python是如何與它縮進的。你認爲我可以通過其他方式改進我的代碼嗎?還是按照原樣完成工作?再次感謝,真的。我很恐慌。 – Amaranthus