gpt4 book ai didi

Python 破译

转载 作者:太空宇宙 更新时间:2023-11-04 06:56:17 27 4
gpt4 key购买 nike

我正在尝试用 Python 破译标准的“a = 1, b = 2, c = 3...”密码,但我有点卡住了。我想要解密的消息是“he”——“8 5”,但由于我的 if 语句的顺序,输出是“eh”。有人知道如何解决这个问题吗?

import re
import sys
message = " 8 5 ";
map(int, re.findall(r'\d+', message))
if "++" in message:
sys.stdout.write(" ")
if "--" in message:
print()
if " 1 " in message:
sys.stdout.write("a")
if " 2 " in message:
sys.stdout.write("b")
if " 3 " in message:
sys.stdout.write("c")
if " 4 " in message:
sys.stdout.write("d")
if " 5 " in message:
sys.stdout.write("e")
if " 6 " in message:
sys.stdout.write("f")
if " 7 " in message:
sys.stdout.write("g")
if " 8 " in message:
sys.stdout.write("h")
if " 9 " in message:
sys.stdout.write("i")
if " 10 " in message:
sys.stdout.write("j")
if " 11 " in message:
sys.stdout.write("k")
if " 12 " in message:
sys.stdout.write("l")
if " 13 " in message:
sys.stdout.write("m")
if " 14 " in message:
sys.stdout.write("n")
if " 15 " in message:
sys.stdout.write("o")
if " 16 " in message:
sys.stdout.write("p")
if " 17 " in message:
sys.stdout.write("q")
if " 18 " in message:
sys.stdout.write("r")
if " 19 " in message:
sys.stdout.write("s")
if " 20 " in message:
sys.stdout.write("t")
if " 21 " in message:
sys.stdout.write("u")
if " 22 " in message:
sys.stdout.write("v")
if " 23 " in message:
sys.stdout.write("w")
if " 24 " in message:
sys.stdout.write("x")
if " 25 " in message:
sys.stdout.write("y")
if " 26 " in message:
sys.stdout.write("z")

最佳答案

如果您使用映射而不是一系列 if 语句,这会变得容易得多:

>>> import string
>>> d = {str(x):y for x,y in enumerate(string.ascii_lowercase,1)}
>>> d['++'] = ' '
>>> d['--'] = '\n'
>>> message = ' 8 5 '
>>> ''.join(d[x] for x in message.split())
'he'

在这里,我使用所有字符串作为字典的键,因为你想支持 '++''--'

关于Python 破译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16718529/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com