缺少1个必要的位置参数 - 凯撒密码

时间:2015-06-24 09:24:44

标签: python encryption

所以我创建了一段将单词转换为新单词的代码(如Caesar Cipher),但是通过这个程序,用户输入一个关键字来改变消息,使用单词的ASCII值将它们改成一个新词。

 exitReq = False
    while not exitReq: 
print ('-' * 45)
print('The Caesar Cipher')
time.sleep(1)
print('Tom Payne 2015')
time.sleep(1)
phrase = input('Please enter the message you want to encrypt: ').lower()
encryption = input("Do you want to [E]ncrypt or [D]ecrypt?: ").upper()
shift_key = input('Please enter the keyword/phrase you would like to be your shift key: ').lower()

def keyword_cipher(key,newletter,phrase):

    if len(phrase) > len(key):
        while len(phrase) > len(key):
            length_to_add = len(phrase) - len(key)
            key = key + key[0:length_to_add]

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

    else:
        pass

    #shift the characters
    shifted_phrase = ''
    if Encryption == ("D"):
        for i in range(len(phrase)):
            newletter == (ord(key[i]) - 96) - (ord(phrase[i]) - 90) + 96

    if Encryption == ("E"):
        for i in range(len(phrase)):
            newletter == (ord(key[i]) - 96) + (ord(phrase[i]) - 96) + 96

    if newletter > 122:
        newletter = chr(newletter - 26)
    else:
        newletter = chr(newletter)

        shifted_phrase = shifted_phrase + new_letter
    return shifted_phrase

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

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)

运行模块时发生的错误是:

Traceback (most recent call last):


 File "S:\Downloads\t2p.py", line 52, in <module>

    result = keyword_cipher(shift_key,phrase);
TypeError: keyword_cipher() missing 1 required positional argument: 'phrase'

1 个答案:

答案 0 :(得分:0)

替换:

def keyword_cipher(key, newletter, phrase):

由:

def keyword_cipher(key,phrase):

我猜您首先编写一个函数来加密单个字母然后更改它以加密字符串并忘记删除字母参数。