【发布时间】:2013-08-19 06:50:14
【问题描述】:
这是我的任务: 编写一个解密秘密消息的程序。
它应该首先提示用户输入加扰的字母表。然后它应该询问秘密消息。最后输出解扰后的版本。
请注意,加扰字母表的输入正好是 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()
msg = input("Now input your scrambled message: ")
print()
print()
num = 0
alphabet = [" "] * 26
for letter in range (26):
alphabet[letter] = letter + 65
while num < 26:
alphabet [num] = decrypt [num]
print(chr(alphabet[num]))
num = num + 1
for alpha in msg.upper():
if alpha < "A" or alpha > "Z":
print(alpha,end="")
else:
print(chr(decrypt[ ord(alpha) - 65 ]), end="")
我似乎不知道如何使用 while 循环来解扰字母表。
目前,如果我输入“XQHAJDENKLTCBZGUYFWVMIPSOR”作为字母表和“VNKW KW BO 1WV WJHFJV BJWWXEJ!”作为秘密消息,程序打印出加扰的字母表和一条“未加扰”的消息,内容为“IZTP TP QG 1PI PLNDLI QLPPSJL!”。解密后的消息实际上是“这是我的第一个秘密消息!”
有帮手吗?
【问题讨论】:
-
我以为你说你的课程在 2 天后结束,2 天前。你还有作业吗?
-
是的。 hw 在决赛后的第二天到期。
标签: python list encryption decode scramble