ex5.py

# -*- coding:utf-8 -*-
"""my_name = 'tingfengyin'
my_age = 35 # not a lie
my_height = 74 #inches
my_weight = 180 #lbs
my_eyes = 'blue'
my_teeth = 'white'
my_hair = 'brown'

print ("let's talk about %s." % my_name)
print ("he's %d inches tall." % my_height)
print ("he's %d pounds heavy." % my_weight)
print ("actually that's not too heavy")
print ("he's got %s eyes and %s hair." % (my_eyes,my_hair))
print ("his teeth are usually %s depending on the coffee."  % my_teeth)

#this line is tricky, try to get it exactly right
print ("if i add %d,%d,and %d i get %d." % (my_age,my_height,my_weight,my_age + my_weight + my_height))"""

name = 'tingfengyin'
age = 35 # not a lie
height = 74 #inches
weight = 180 #lbs
eyes = 'blue'
teeth = 'white'
hair = 'brown'

print ("let's talk about %s." % name)
print ("he's %d inches tall." % height)
print ("he's %r pounds heavy." % weight)
print ("actually that's not too heavy")
print ("he's got %s eyes and %s hair." % (eyes,hair))
print ("his teeth are usually %s depending on the coffee."  % teeth)

#this line is tricky, try to get it exactly right
print ("if i add %r,%s,%s,and %d i get %r." % (age,height,weight,age + weight + height))
#%s对应的位置应该放字符串,但是,如果放了整数,也可以。只不过是已经转为字符串对待了。但是不赞成这么做。
#%d对应的是放一个数字,而不是放一个字符,否则也会报错TypeError: %d format: a number is required, not str
#%r 用于调试比较好,它会显示原始数据,比如如果是字符,就会显示为'字符'
#TypeError: not enough arguments for format string 出现这个错误说明字符串里的%格式化字符数量比后面的多了

 

posted @ 2016-11-14 14:34  听风呤  阅读(140)  评论(0编辑  收藏  举报