'''
恺撒密码 I
描述
凯撒密码是古罗马凯撒大帝用来对军事情报进行加解密的算法,它采用了替换方法对信息中的每一个英文字符循环替换为字母表序列中该字符后面的第三个字符,即,字母表的对应关系如下:
原文: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
密文:D E F G H I J K L M N O P Q R S T U V W X Y Z A B C
对于原文字符P,其密文字符C满足如下条件:C=(P+3) mod 26
上述是凯撒密码的加密方法,解密方法反之,即:P=(C-3) mod 26
假设用户可能使用的输入仅包含小写字母a~z和空格,请编写一个程序,对输入字符串进行凯撒密码加密,直接输出结果,其中空格不用进行加密处理。使用input()获得输入。
输入
示例1: python is good
输出
示例1: sbwkrq lv jrrg
'''
#<1>
Str = input()
for i in range(0, len(Str)):
if Str[i] == ' ':
print(' ', end="")
elif Str[i] in ['x', 'y', 'z']:
# print('{}'.format(chr(ord(Str[i]) - 23)), end="") #另一种写法
print(chr(ord(Str[i])-23),end='')
else:
# print('{}'.format(chr(ord(Str[i]) + 3)), end="") #另一种写法
print(chr(ord(Str[i])+3),end='')
#<1>的另一种表达,可以解释“逻辑或只能 “A or B”不能“A or B or C””
  1. shuru=input()
  2. n=len(shuru)
  3. cheng=' '
  4. for i in range(n):
  5. if shuru[i]=='X'or shuru[i]=='x':
  6. cheng+=chr(ord(shuru[i])-23)
  7. elif shuru[i]=='Y' or shuru[i]=='y':
  8. cheng+=chr(ord(shuru[i])-23)
  9. elif shuru[i]=='Z' or shuru[i]=='z':
  10. cheng+=chr(ord(shuru[i])-23)
  11. elif shuru[i]==' ':
  12. cheng+=shuru[i]
  13. else:
  14. cheng+=chr(ord(shuru[i])+3)
  15. print(cheng[1:n+1])
#<2>
#p="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"#这道题目中这样写也可以
p="abcdefghijklmnopqrstuvwxyz"
n = input()
for i in n:
if i ==" ":#判断为空格特殊情况
print(" ", end="")
else:
a=p.find(i)#查找索引位置
b=(a+3)%26
print(p[b],end="")
#<3>
P = input()
G = []
for i in P:
G.append(i)
L = len(G)
K = ["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"]
for i in range(L):
if G[i] in K:
x = K.index(G[i])
if x == 23:
x = 0
elif x == 24:
x = 1
else:
x = x + 3
print(K[x],end="")
else:
print(" ",end="")
#<4>这样写大写也可以转换成对应的大写字母
P = input()
G = []
for i in P:
G.append(i)
L = len(G)
K = ["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",'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']
for i in range(L):
if G[i] in K:
x = K.index(G[i])
if x == 23:
x = 0
elif x == 24:
x = 1
elif x == 25:
x = 3
elif x == 49:
x = 26
elif x == 50:
x = 27
elif x == 51:
x = 28
else:
x = x + 3
print(K[x],end="")
else:
print(" ",end="")
#<5>这样写也可以转换直接转换对应的大小写
Str = input()
for i in range(0, len(Str)):
if Str[i] == ' ':
print(' ', end="")
elif Str[i] in ['x', 'y', 'z','X','Y','Z']:
# print('{}'.format(chr(ord(Str[i]) - 23)), end="") #另一种写法
print(chr(ord(Str[i])-23),end='')
else:
# print('{}'.format(chr(ord(Str[i]) + 3)), end="") #另一种写法
print(chr(ord(Str[i])+3),end='')
#方法<2>不好实现大写对应转换,带可以启示稍稍改进方法<4>
P = input()
G = []
for i in P:
G.append(i)
L = len(G)
K = ["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",'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']
for i in range(L):
if G[i] in K:
x = K.index(G[i])
if x == 23 or x == 24 or x == 25:
x = (x+3)%26
elif x == 49:
x = 26
elif x == 50:
x = 27
elif x == 51:
x = 28
else:
x = x + 3
print(K[x],end="")
else:
print(" ",end="")
05-11 09:38