下面据说是一个凯撒密码的第二个版本(向右迁移13位)
s='My name is SuperLadyMa'
ss='Zl anzr vf FhcreYnqlZn'
d=dict()
for c in (65,97):
for i in range(26):
d[chr(i+c)]=chr((i+13)%26+c)
print(d)
print(''.join(d.get(c,c) for c in s))
print(''.join(d.get(c,c) for c in ss))
#如果写get(c) 出现d.get(' ') 即此key不存在于字典中 则会返回一个NoneType
#d.get(' ',' ')返回str' '
特意试了一下d.get(c) 结果报了一个错:
Traceback (most recent call last):
File "C:/Users/h'w/Desktop/python_work/problemch3.py", line 8, in <module>
print(''.join(d.get(c) for c in s))
TypeError: sequence item 2: expected str instance, NoneType found
简单测试了一下发现使用字典中没有的键值,就会返回NoneType,所以直接使用
d.get(c,c)最好,这样会返回str c 如果是两个不同的str:d.get(a,b)
ab都为str且均不为d中键值,返回b