-1

这是我的任务:

编写一个解密秘密消息的程序。

它应该首先提示用户输入加扰的字母表。然后它应该询问秘密消息。最后,它输出未加扰的版本。

请注意,加扰字母表正好输入 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循环,但我不知道如何使用一个来解码字母表。

有帮手吗?

4

2 回答 2

2

好吧,正如已经指出的那样,您正在破坏您的decrypt变量。但是,您也没有正确/根本没有构建从“加扰”字母表到常规字母表的映射。

除了对列表和简单列表函数index()

cleartext = ""
for c in msg:
    if c in alphabet:
        pos = alphabet.index(c)
        cleartext += string.ascii_uppercase[pos]
    else:
        cleartext += c

它不仅可以修补您的代码,还可以从一些改进中受益。我认为这可能是家庭作业,您可能不会走这么远,但是 IMO 无论如何学习它并没有错。

  • 您没有检查输入字母表是否只包含您认为是合法值的任何内容(例如,在这种情况下可能是 AZ ),也没有检查重复项。用户可以输入任何旧垃圾,否则会破坏您的程序。
  • 您的打印和循环不是很惯用
  • 函数有助于将代码分解成更易于阅读和维护的部分。
  • 这可能会让人觉得老派或迂腐,但不推荐使用超过 80 个字符的行用于 Python。将连接相邻的字符串文字(例如“one”“two”)(甚至跨换行符)。

如果我不得不做你正在做的事情translate(见下文),我可能会做这样的事情(只是一个简单的例子,可能会通过一些工作来改进):

import string

def validate(alpha):
    # Is it exactly 26 characters long?
    if len(alpha) != 26: return False
    for c in alpha:
        # Is every character in [A-Z]?
        if c not in string.ascii_uppercase: return False
        # Is this character duplicated?
        if alpha.count(c) > 1: return False
    return True


alphabet = ""
while not validate(alphabet):
    alphabet = input("Please input the encryption alphabet in order (only A-Z"
                     " allowed, with no duplicates): ")

msg = input("Now input your encrypted message: ")

print("Your encrypted alphabet is:", alphabet)
print("Your encrypted message is:", msg)

# Create a mapping from one alphabet to the other using a dictionary
table = dict(zip(alphabet, string.ascii_uppercase))
cleartext = "".join([table[c] if c in table else c for c in msg])

print("Your decrypted message reads:", cleartext)

最后,您还可以使用 Python 的内置字符串翻译来执行此操作,如下所示:

import string
# read and validate alphabet
# read message
print(str.translate(message, str.maketrans(alphabet, string.ascii_uppercase)))
于 2013-08-16T05:54:44.060 回答
1

在将所有乱码写入其中后,您正在破坏解密:

crypt = ["*"] * 26
for s in range (26):
    val = decrypt[s] - 65
    crypt[val] = (s + 65)
于 2013-08-16T04:29:06.727 回答