python - print("%s's %s is %s." % \) 有具体代码,请问这种怎么解释?
天蓬老师
天蓬老师 2017-04-18 10:13:21
0
3
2719
# -*- 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])
    怎么解释呢?运行还报错了
天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

Antworte allen(3)
阿神

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

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]))
PHPzhong
if name in people:
    print("{0}'s {1} is {2}.".format(name, labels[key], people[name][key]))
巴扎黑

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]))
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage