people={
"alice":{
"phone":"2341",
"addr":"for drive 23"
},
"beth":{
"phone":"9012",
"addr":"bsr street 42"
},
"cecil":{
"phone":"3158",
"addr":"baz avenue 90"
}
}
#针对电话号码和地址使用的描述性标签,会在打印时候用到
labels={
"phone":"phone number",
"addr":"address"
}
name=raw_input("Name:")
#查找电话号码还是地址?
request=raw_input("phone number(p) or address (a)?")
#使用正确的键:
if request=="p":key="phone"
if request=="a":key="addr"
#如果名字是字典中的有效键方才打印信息:
if name in people:print "%s's %s is %s."%\
(name.labels[key],people[name][key])
提示错误为:
File "c:\Users\DULU\Desktop\untitled-1.py", line 32, in
(name.labels[key],people[name][key])
AttributeError: 'str' object has no attribute 'labels'
(这是什么意思,看不懂呀) |