码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2014-07-02 19:06:24      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:blog   for   python   div   学习   res   

#!/usr/bin/python
#FileName:caesar.py
#Code:UTF-8

import sys

def 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 ChgString

def 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 string

if __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

  

python学习笔记[1]-凯撒密码,布布扣,bubuko.com

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

标签:blog   for   python   div   学习   res   

原文地址:http://www.cnblogs.com/lvsdz/p/3819603.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!