Python实现恺撒密码(8行代码)
程序员文章站
2022-03-11 14:52:07
直接上代码(精简版)#字母对应数字dic = {"a":0,"b":1,"c":2,"d":3,"e":4,"f":5,"g":6,"h":7,"i":8,"j":9,"k":10,"l":11,"m":12,"n":13,"o":14,"p":15,"q":16,"r":17,"s":18,"t":19,"u":20,"v":21,"w":22,"x":23,"y":24,"z":25}#打印时使用该字符串cha = "abcdefghijklmnopqrstuvwxyz"list1 = li....
直接上代码(精简版)
#字母对应数字
dic = {"a":0,"b":1,"c":2,"d":3,"e":4,"f":5,"g":6,"h":7,"i":8,"j":9,"k":10,"l":11,"m":12,"n":13,"o":14,"p":15,"q":16,"r":17,"s":18,"t":19,"u":20,"v":21,"w":22,"x":23,"y":24,"z":25}
#打印时使用该字符串
cha = "abcdefghijklmnopqrstuvwxyz"
list1 = list(input("\n请输入一串字母:"))
i = eval(input("请输入偏移量(0-26):"))
if i >=0 and i <=25:#偏移并直接输出
print("偏移量%d:" % (i),end=" ")
for j in list1:
#python三目运算符(条件为真时的结果 if 判段的条件 else 条件为假时的结果)
print(cha[dic.get(j)+i - 26 if (dic.get(j)+i > 25) else dic.get(j)+i],end="")
运行结果
本文地址:https://blog.csdn.net/weixin_44864260/article/details/109269098