凯撒密码换新线

时间:2017-04-12 18:02:23

标签: python-3.x caesar-cipher

我的代码尝试进行高级caesar密码转换时出现问题。我将某个字母更改为另一个字母,然后在编码之前将一个字符串添加到文件的某些部分,但现在正在进行移位时遇到问题。这是我的代码:

import string
import sys
count = 1
cont_cipher = "Y"

#User inputs text
while cont_cipher == "Y":
    if count == 1:
        file = str(input("Enter input file:" ""))
        k = str(input("Enter shift amount: "))
        purpose = str(input("Encode (E) or Decode (D) ?: "))

    #Steps to encode the message, replace words/letter then shift        
        if purpose == "E":
            my_file = open(file, "r")
            file_contents = my_file.read()

            #change all "e"s to "zw"s 
            for letter in file_contents:
                if letter == "e":
                    file_contents = file_contents.replace(letter, "zw")

            #add "hokie" to beginning, middle, and end of each line
            lines = file_contents.split('\n')
            def middle_message(lines, position, word_to_insert):
                lines = lines[:position] + word_to_insert + lines[position:]
                return lines
            new_lines = ["hokie" + middle_message(lines[x], len(lines[x])//2, "hokie") + "hokie" for x in range(len(lines))]

        #math to do the actual encryption
        def caesar(word, shifts):
           word_list = list(new_lines)
           result = []
           s = 0
           for c in word_list:
               next_ord = ord(c) + s + 2
               if next_ord > 122:
                   next_ord = 97
               result.append(chr(next_ord))
               s = (s + 1) % shifts
           return "".join(result)

        if __name__ == "__main__":
            print(caesar(my_file, 5))

        #close file and add to count
        my_file.close()
        count = count + 1

我得到的错误是:

TypeError: ord() expected a character, but string of length 58 found

我知道我需要将其拆分为单个字符,但我不知道该怎么做。我需要在此步骤中使用更新的消息而不是原始文件...

0 个答案:

没有答案