# -*- coding:utf-8 -*-
#字典使用人名作为键,每个人用另一个字典表示
people = {
'mary' : {
'phone' : '123456',
'addr' : 'xingfudadao123456'
},
'alice' : {
'phone' : '890123',
'addr' : 'xingfudadao890123'
},
'cecil' : {
'phone' : '456789',
'addr' : 'xingfudadao456789'
}
}
labels = {
'phone' : 'phone_number',
'addr' : 'address'
}
name = input('Name:')
request = 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])
#请问最后两句代码:if name in people:print("%s's %s is %s." % \)
(name,labels[key],people[name][key])
怎么解释呢?运行还报错了
阿神2017-04-18 10:15:21
这是格式化输出,这样写看的懂了吧。
if name in people:
print("%s's %s is %s." % \
(name,labels[key],people[name][key]))
再有疑惑这样写。
if name in people:
print("%s's %s is %s." % (name, labels[key], people[name][key]))