这是我的任务:
编写一个解密秘密消息的程序。
它应该首先提示用户输入加扰的字母表。然后它应该询问秘密消息。最后,它输出未加扰的版本。
请注意,加扰字母表正好输入 26 个字符。所有字母字符都被翻译成它们的解码等价物(这将需要一个 WHILE 循环),并且所有其他非字母字符应该完全按照它们没有翻译的方式输出。
到目前为止,这是我的代码:
decrypt = ["*"] * 26
scram_alphabet = input("Please input the scrambled alphabet in order: ")
while len(scram_alphabet) != 26:
scram_alphabet = input("Please input the scrambled alphabet in order. The alphabet must have 26 characters: ")
num = 0
for each_letter in scram_alphabet:
decrypt[num] = ord(each_letter)
num = num + 1
print()
print("Your scrambled alphabet is: ", end = "")
for num in range (26):
print(chr(decrypt[num]), end = "")
print()
print()
msg = input("Now input your scrambled message: ")
print()
print()
alphabet = 65
for s in range (26):
decrypt[s] = (alphabet)
alphabet = alphabet + 1
print("The unscrambled alphabet is: ", end = "")
for num in range (26):
print(chr(decrypt[num]), end = "")
print()
print()
print("Your unscrambled message reads: ")
for alpha in msg.upper():
if alpha < "A" or alpha > "Z":
print(alpha, end="")
else:
ord_alpha = ord(alpha)
print (chr(decrypt[ord_alpha - 65]), end = "")
例如:加扰字母 = XQHAJDENKLTCBZGUYFWVMIPSOR,加扰消息 = VNKW KW BO 1WV WJHFJV BJWWXEJ!
一切正常,直到我到达最后一个打印语句,它说未加扰的消息与加扰的消息相同。我知道指令需要一个while循环,但我不知道如何使用一个来解码字母表。
有帮手吗?