热线电话:13121318867

登录
2018-10-23 阅读量: 820
python错误解析(一)
>>> D1 = {'x':1, 'y':2}  
>>> D1['x']
1
>>> D1['z']

Traceback (most recent call last):  
File "<pyshell#185>", line 1, in <module>
D1['z']
KeyError: 'z'

【错误分析】这是Python中字典键错误的提示,如果想让程序继续运行,可以用字典中的get方法,如果键存在,则获取该键对应的值,不存在的,返回None,也可打印提示信息.

代码如下:

>>> D1.get('z', 'Key Not Exist!')  
'Key Not Exist!'
0.0000
4
关注作者
收藏
评论(0)

发表评论

暂无数据
推荐帖子