0

首先,这是代码:

    alphabet = input("Please input the scrambled alphabet in order: ")
    message = input("Now input the scrambled message: ")
    secret_map = {}
    for index, letter in enumerate(alphabet):
        secret_map[letter] = chr(65 + index)
    new_str = ("".join([secret_map[char] for char in message]))
    print(new_str, end="")

OUTPUT
Please input the scambled alphabet in order = DRLOWNUKSBZQJHCTXMIFYGPAEV
Now input the scrambled message = LOWNUK
CDEFGH

Please input the scambled alphabet in order = DRLOWNUKSBZQJHCTXMIFYGPAEV
Now input the scrambled message = LOWNUK LOWNUK
OUTPUT = 

    Traceback (most recent call last):
      File "/Users/sanjith/Documents/assign8t.py", line 6, in <module>
        new_str = ("".join([secret_map[char] for char in message]))
      File "/Users/sanjith/Documents/assign8t.py", line 6, in <listcomp>
        new_str = ("".join([secret_map[char] for char in message]))
    KeyError: ' '

我很难获得空间。我知道它与空间的 ascii 编号 32(从之前的 127 更改)有关。

4

1 回答 1

0

dict.get().

secret_map.get(char, char)
于 2013-08-15T01:32:40.927 回答