people = { 'Alice': { 'phone': '2341', 'addr': 'Foo drive 23' }, 'Beth': { 'phone': '19102', 'addr': 'Bar street 42' }, 'Cecil': { 'phone': '3158', 'addr': 'Baz avenue 90' } } 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 {} is {}.".format(name, labels[key], people[name][key]))
根据构建的字典输入姓名
例如:Beth
再输入要查找的信息,手机号或地址(p or a) 即可获取相应信息
“ { } ”.format( )索引替换