まず、コードは次のとおりです。
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から変更)と関係があることはわかっています。