1 code = 'g fmnc wms bgblr rpylqjyrc gr zw fylb. rfyrq ufyr amknsrcpq ypc dmp.\
2 bmgle gr gl zw fylb gq glcddgagclr ylb rfyrq ufw rfgq rcvr gq qm jmle.\
3 sqgle qrpgle.kyicrpylq() gq pcamkkclbcb. lmu ynnjw ml rfc spj.'
4
5 decode = []
6 for i in code:
7 if ord('a') <= ord(i) <= ord('x'):
8 decode.append(chr(ord(i) + 2))
9 elif ord('y') <= ord(i) <= ord('z'):
10 decode.append(chr(ord(i) - 24))
11 else:
12 decode.append(i)
13 print(''.join(decode))
View Code
运行后输出了转换后的提示:i hope you didnt translate it by hand. thats what computers are for.doing it in by hand is inefficient and thats why this text is so long.using string.maketrans() is recommended. now apply on the url.
果然按部就班的做完还是有好处的,提示里提到了string.maketrans()这个函数。根据python的官方文档说明,string.maketrans(from,to)函数给translate()函数提供一个从from映射到to的翻译表,而translate(s,table,[deletechars])函数则是从s中删除出现在deletechars里(如果有的话)的字符,再根据table的的规则进行转换。
于是使用string下的maketrans()和translate()的代码如下: