php的Python语法(KeyError除外)
我使用一个php的Python语法(KeyError除外),python,Python,我使用一个python模块,并为php重写它。我不能理解的含义,除了keyrerror。如何在php中实现它 Python:(最后一个else的位置有点奇怪) 我找了,但没找到。有什么想法吗?当尝试天真地(即不使用字典的get方法)访问字典中不存在的密钥时,会引发keyrerror异常: dic = {'a': 1, 'b': 2} print(dic['c']) # KeyError: 'c' 要捕获此异常,请使用尝试捕获键错误的除外: dic = {'a': 1, 'b': 2} try:
python
模块,并为php
重写它。我不能理解的含义,除了keyrerror
。如何在php中实现它
Python:(最后一个else的位置有点奇怪)
我找了,但没找到。有什么想法吗?当尝试天真地(即不使用字典的
get
方法)访问字典中不存在的密钥时,会引发keyrerror
异常:
dic = {'a': 1, 'b': 2}
print(dic['c'])
# KeyError: 'c'
要捕获此异常,请使用尝试捕获键错误的除外:
dic = {'a': 1, 'b': 2}
try:
print(dic['c'])
except KeyError:
print("'c' is not in the dictionary")
# 'c' is not in the dictionary
你的PHP代码在哪里?向我们展示您迄今为止的尝试。我搜索了,但没有找到。
很抱歉,但我真的怀疑:@KhorneHoly我甚至不了解keyrerror
。抱歉,我对python非常陌生。@DeepSpace我发现了,但不理解其含义:(
dic = {'a': 1, 'b': 2}
try:
print(dic['c'])
except KeyError:
print("'c' is not in the dictionary")
# 'c' is not in the dictionary