首页 > 代码库 > python学习笔记[1]-凯撒密码

python学习笔记[1]-凯撒密码

#!/usr/bin/python#FileName:caesar.py#Code:UTF-8import sysdef caesarEnCode(string,tag):    ChgString = ""    for s in string:        if s.islower():            ChgString += chr((ord(s)-ord(‘a‘)+tag)%26+ord(‘a‘))        elif s.isupper():            ChgString += chr((ord(s)-ord(‘A‘)+tag)%26+ord(‘A‘))    return ChgStringdef caesarDeCode(string,tag):    ChgString = ""    for s in string:        if s.islower():            ChgString += chr((ord(s)-ord(‘a‘)+tag)%26+ord(‘a‘))        elif s.isupper():            ChgString += chr((ord(s)-ord(‘A‘)+tag)%26+ord(‘A‘))    return ChgString    return stringif __name__==‘__main__‘:    flag=sys.argv[1]    string=sys.argv[2]        tag = 3    if flag == ‘E‘:        restring = caesarEnCode(string,tag)    elif flag == ‘D‘:        restring = caesarDeCode(string,tag)print restring