我的程序中的加密方法没有正确加密。我以为我明白了为什么要使用调试模式;这是因为它将单词之间的空格读取为必须加密的内容。所以我尝试输入一条没有空格的消息,但它仍然没有正确输出。我认为问题在于带有键的 if 语句。我尝试将行注释掉,更改语句,将 if 语句更改为 for 循环,但它仍然不正确。def main(): vig_square = create_vig_square() message = input("Enter a multi-word message with punctuation: ") input_key = input("Enter a single word key with no punctuation: ") msg = message.lower() key = input_key.lower() coded_msg = encrypt(msg, key, vig_square) print("The encoded message is: ",coded_msg) print("The decoded message is: ", msg) def encrypt(msg,key,vig_square): coded_msg = "" key_inc = 0 for i in range(len(msg)): msg_char = msg[i] if key_inc == len(key)-1: key_inc = 0 key_char = key[key_inc] if msg_char.isalpha() and key_char.isalpha(): row_index = get_row_index(key_char,vig_square) col_index = get_col_index(msg_char,vig_square) coded_msg = coded_msg+vig_square[row_index][col_index] else: coded_msg = coded_msg + " " key_inc = key_inc+1 return coded_msg 但是我的编码信息是:epr iloyo sif plvqoh
1 回答
![?](http://img1.sycdn.imooc.com/533e4d470001a00a02000200-100-100.jpg)
茅侃侃
TA贡献1842条经验 获得超21个赞
你有两个错误:
首先,您不要使用密钥中的所有字符。更改以下行:
if key_inc == len(key)-1:
key_inc = 0
到
if key_inc == len(key):
key_inc = 0
其次,即使您处理消息中的非字母字符(例如空格),您也会移动键指针。仅当您对字符进行编码时才这样做,即进行以下更改:
if msg_char.isalpha() and key_char.isalpha():
...
key_inc = key_inc+1 # Move this line here
else:
...
添加回答
举报
0/150
提交
取消