这里的概念是使用字典数据结构进行编码和解码。有人能帮我把这件事做得更好更简单吗? 或者是否有使用字典数据结构执行编码和解码的内置函数
qwerty_encrypt={'a':'q','b':'w','c':'e','d':'r','e':'t','f':'y','g':'u','h':'i','i':'o','j':'p','k':'a','l':'s','m':'d','n':'f','o':'g','p':'h','q':'j','r':'k','s':'l','t':'z','u':'x','v':'c','w':'v','x':'b','y':'n','z':'m','1':'9','2':'8','3':'7','4':'6','5':'5','6':'4','7':'3','8':'2','9':'1','0':'0'}
msg=input('enter yuor msg:').lower()
#encrypt_func
def enigma_encrypt(msg):
a=list(msg.split(' '))
li=[]
for i in a:
li.append(list(i))
li2=[]
li4=[]
for i,j in enumerate(li):
li3=[]
for k,l in enumerate(j):
li3.append(qwerty_encrypt[l])
li2.append(li3)
for m in li2:
li4.append(''.join(m))
after_encrypt=(' '.join(li4))
return after_encrypt
def enigma_decrypt(msg):
a=list(msg.split(' '))
li=[]
for i in a:
li.append(list(i))
li2=[]
li4=[]
for i,j in enumerate(li):
li3=[]
for k,l in enumerate(j):
for n in qwerty_encrypt:
if qwerty_encrypt[n]==l:
li3.append(n)
li2.append(li3)
for m in li2:
li4.append(''.join(m))
after_encrypt=(' '.join(li4))
return after_encrypt
print('After encrytion:',enigma_encrypt(msg),'\nAfter decryption:',enigma_decrypt(enigma_encrypt(msg)))
可以将str.translate与str.maketrans结合使用:
我更改了qwerty\u加密,将null添加为空格。 是这样的:
事情就是这样:
尽管@myrmica代码简单易懂
但这是我的尝试,同样的方式,少名单。我希望这是你所期望的
相关问题 更多 >
编程相关推荐