gpt4 book ai didi

带字母表的 Python 移位

转载 作者:行者123 更新时间:2023-11-28 16:27:36 28 4
gpt4 key购买 nike

<分区>

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()

嗨,我无法让我的代码工作,我的编码功能可以工作,但我正在努力修复我的解码功能。我不明白我哪里出错了。我希望能够解码我将编码的消息,但这不起作用,因为解码功能会停止程序。我在编码中犯了错误吗?感谢所有的帮助问候

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com