最近学习Python,看到大家推荐的一些带有闯关和游戏性质的Python学习网站,其中就包括pythonchallenge。
于是尝试着记录下来自己的一步步学习的过程,把相关的代码汇总和记录下来。下面是我个人尝试的代码:
ref = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
def decode(message):
List = list(message)
mseg = ""
for ch in List:
if ch in ref:
#print ref.index(ch)
mseg += ref[(ref.index(ch)+2)%26]
else:
mseg += ch
return mseg
message = "g fmnc wms bgblr rpylqjyrc gr zw fylb. rfyrq ufyr amknsrcpq ypc dmp. bmgle gr gl zw fylb gq glcddgagclr ylb rfyr'q ufw rfgq rcvr gq qm jmle. sqgle qrpgle.kyicrpylq() gq pcamkkclbcb. lmu ynnjw ml rfc spj."
mesg = decode(message)
print str(mesg)
本关推荐使用
string.maketrans用来建立一个字符的映射表,结合字符的方法translate做非常方便.
下面是translate的解释:
translate(...) S.translate(table [,deletechars]) -> string Return a copy of the string S, where all characters occurring in the optional argument deletechars are removed, and the remaining characters have been mapped through the given translation table, which must be a string of length 256 or None. If the table argument is None, no translation is applied and the operation simply removes the characters in deletechars