0
# -*- 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])
    怎么解释呢?运行还报错了

查看全部 3 个回答

0

pyformat

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]))
    #print("{}'s {} is {}.".format(name,labels[key],people[name][key]))

推荐答案

1

这是格式化输出,这样写看的懂了吧。

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]))
SegmentFault

一起探索更多未知

下载 App