[python]rot13练习
作者:互联网
code:
def rot13(s):
intab=''
outtab=''
for a in range(ord('A'),ord('Z')+1):
intab+=chr(a)
for a in range(ord('a'),ord('z')+1):
intab+=chr(a)
for b in range(ord('N'),ord('Z')+1):
outtab+=chr(b)
for b in range(ord('A'),ord('N')):
outtab+=chr(b)
for b in range(ord('n'),ord('z')+1):
outtab+=chr(b)
for b in range(ord('a'),ord('n')):
outtab+=chr(b)
trantab=str.maketrans(intab,outtab)
return s.translate(trantab)
if __name__ == '__main__':
s1='Yvsr vf cngurgvp, yrg\'f clgubavp!'
r1=rot13(s1)
print(r1)
print(s1==rot13(r1))
s2='Jvgu terng cbjre,pbzrf terng erfobafvovyvgl!'
r2=rot13(s2)
print(r2)
print(s2==rot13(r2))
结果:
end
标签:__,outtab,python,练习,chr,rot13,range,ord 来源: https://blog.csdn.net/weixin_44321080/article/details/102758821