Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/317.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 在输入字符串中移动值_Python - Fatal编程技术网

Python 在输入字符串中移动值

Python 在输入字符串中移动值,python,Python,我正试着做一个凯撒密码,我已经很接近了。问题是我的输出只打印出一个字母,而不是整行加密。E是英语中使用频率最高的字母,因此我们的想法是找出E和输入中使用频率最高的字母之间的距离。然后把所有东西移到那个距离。我是python新手,所以还不是很好。非常感谢!以下是我目前掌握的代码: maximum_character = unciphered_text[0] maximum_count = unciphered_text.count(unciphered_text[0]) for char in u

我正试着做一个凯撒密码,我已经很接近了。问题是我的输出只打印出一个字母,而不是整行加密。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)

您在
caesar\u 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 

成功了!非常感谢你!我不知道python的缩进有多敏感。你认为我可以用其他方法改进我的代码吗?或者它能按原样完成工作吗?再次感谢你,真的。我很惊慌。