Caesar密码发生错误

时间:2015-06-22 10:24:38

标签: python encryption

我的计算机科学任务之一是使用关键字作为班次来创建凯撒密码,我一直在努力修复一个不断发生的错误,我正在努力解决它。

import time

exitReq = False
while not exitReq:
############################################################################    

    print ('-' * 45)
    print('The Caesar Cipher')
    time.sleep(1)
    print('Tom Payne 10B 2015')
    time.sleep(1)
    #Asks the user to enter all the information needed
    message = input('Please enter the phrase you want to encrypt: ')
    Encryption = ("Do you want to [E]ncrypt or [D]ecrypt") 
    shift_key = input('Please enter the keyword/phrase you would like to be your shift key : ')


     #Encyrpt
    def keyword_cipher(key, message):
        if Encryption == ("E"):
            if len(message) > len(key):
                while len(message) > len(key):
                    length_to_add = len(message) - len(key)
                    key = key + key[0:length_to_add]

            elif len(message) < len(key):
                while len(message) < len(key):
                    length_to_sub = len(key) - (len(key) - len(message))
                    key = key[0:length_to_sub]
        #Decrypt
        if Encryption == ("D"):
            if len(message) > len(key):
                while len(message) > len(key):
                    length_to_add = len(message) - len(key)
                    key = key - key[0:length_to_add]

        elif len(message) < len(key):
            while len(message) < len(key):
                length_to_sub = len(key) - (len(key) - len(message))
                key = key[0:length_to_sub]

        else:
            pass

        #shift the characters
        shifted_phrase = ''
        for i in range(len(message)):
            new_letter = (ord(key[i]) - 96) + (ord(message[i]) - 96) + 96
            if new_letter > 122:
                new_letter = chr(new_letter - 26)

            else:
                new_letter = chr(new_letter)


            shifted_phrase = shifted_phrase + new_letter
        return shifted_phrase


    #call it all to action
    result = keyword_cipher(shift_key, message)

    print ('Encrypted message:')
    print (result)

###############################################################################
    print ('=' * 45)
    time.sleep(1)
    exit = input('Would you like to exit: ')
    if (exit) == 'yes':
            exitReq = True
            print('Thank You. Goodbye')
            print ('-' * 45)


    if (exit) == 'no':
        exitReq = False

每当我尝试运行代码时,都会发生此错误:

    Traceback (most recent call last):

File "S:\Downloads\t2p.py", line 70, in <module>
    result = keyword_cipher(shift_key, message)
TypeError: keyword_cipher() missing 1 required positional argument: 'message'

0 个答案:

没有答案