【发布时间】:2016-02-03 13:16:23
【问题描述】:
def menu():
choice = input("Press 1 to encode, 2 to decode, 9 to exit ")
return choice
def makeKeycode(message):
key = input("What is the key? ").upper()
length = len(message)
keycode = ""
counter = 0
while length >0:
if counter == len(key):
counter = 0
keycode = keycode + key[counter]
counter = counter + 1
length = length - 1
print(keycode)
return keycode
def enterMessage():
message = input("What is the message ").upper()
return message
def encodeMessage(message, keycode):
ciphertext =""
alphabet= "ABCDEFGHIJKLMNOPQRTUVWXYZ"
for i in range (len(message)):
character = message[i]
charCode = alphabet.find(character)
keycodeChar =keycode[i]
keyLetter = alphabet.find(keycodeChar)
position = (charCode + keyLetter)%25
cipherLetter = alphabet[position]
ciphertext =ciphertext + cipherLetter
return ciphertext
def decodeMessage(ciphertext,keycode):
ciphertext =""
alphabet= "ABCDEFGHIJKLMNOPQRTUVWXYZ"
for i in range (len(ciphertext)):
character = ciphertext[i]
charCode = alphabet.find(character)
keycodeChar =keycode[i]
keyLetter = alphabet.find(keycodeChar)
position = (charCode - keyLetter)%25
cipherLetter = alphabet[position]
ciphertext =ciphertext - cipherLetter
return message
def enterCipher ():
ciphertext = input("Enter the text to be decoded")
return ciphertext
def encode():
message = enterMessage()
keycode = makeKeycode(message)
ciphertext = encodeMessage(message,keycode)
print(ciphertext)
def decode():
keycode = makeKeycode(ciphertext)
message = decodeMessage(ciphertext, keycode)
print (message)
def main():
MyDictionary=("A:1","B:2","C:3","D:4","E:5","F:6","G:7","H:8","I:9","J:10","K:11","L:12","M:13","N:14","O:15","P:16","Q:17","R:18","S:19","T:20","U:21","V:22","W:23","X:24","Y:25","X:26")
print (MyDictionary)
choice = 0
while choice !=9:
choice = int(menu())
if choice == 1:
encode()
elif choice == 2:
decode()
if __name__ == "__main__":
main()
嗨,我无法让我的代码工作,我的编码功能可以工作,但我正在努力修复我的解码功能。我不明白我哪里出错了。我希望能够解码我将编码的消息,但这不起作用,因为解码功能会停止程序。我在编码中犯了错误吗?感谢所有的帮助 问候
【问题讨论】:
-
好吧,这里有个问题,如果你的编码信息是“11”,那是“K”还是“AA”?
-
当您说您的解码消息停止程序时,您遇到了什么错误?究竟是什么问题?
-
请不要破坏您的帖子。请注意,一旦您在本网站上发布了问题或答案,这些帖子就会成为也为该内容做出贡献的其他人的集体努力的一部分。除非在特殊情况下,否则不应删除可能对其他人有用的帖子。即使该帖子对原作者不再有用,该信息仍然对将来可能遇到类似问题的其他人有益 - 这是 Stack Exchange 的基本理念。