相关代码为here on Github。基本上,一切都在工作,除了addClient( )函数,它接受一个SRE_Match对象并对其进行解析,然后将提取的变量分配给全局定义的dict中的键。但是在创建了这个新密钥之后,当它打印出与该密钥相关联的项时,它会抛出一个KeyError,说明该密钥没有定义!我不知道这是怎么回事。
addClient()的代码:
def addClient(client, info,medium):
if info:
info = info.group(0).split(' ')
if medium == 'text':
db['clients']['text'][client] = {'number':info[0],'os':info[1]}
elif medium == 'email':
db['clients']['email'][client] = {'address':info[0],'os':info[1]}
elif medium == 'pm':
db['clients']['pm'][client] = {'os':info[0]}
print(db['clients'][medium][client])
答案 0 :(得分:0)
好的,答案应该很明显。正如TesselatingHeckler建议的那样,当info
不成立时会弹出错误,并且可以修复如下:
def addClient(client, info,medium):
if info:
info = info.group(0).split(' ')
if medium == 'text':
db['clients']['text'][client] = {'number':info[0],'os':info[1]}
elif medium == 'email':
db['clients']['email'][client] = {'address':info[0],'os':info[1]}
elif medium == 'pm':
db['clients']['pm'][client] = {'os':info[0]}
print(db['clients'][medium][client])