TypeError: sequence item 2: expected str instance, NoneType found

下面据说是一个凯撒密码的第二个版本(向右迁移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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值