运用占位符来进行格式化输出
代码如下:
1 #__ author:"Cheng" 2 #date:2018-02-12 3 4 name = input("请输入您的姓名:") 5 age = input("请输入您的年龄:") 6 job = input("您的工作:") 7 salary = input("您的薪资是多少:") 8 9 10 if age.isdigit(): 11 age = int(age) 12 else: 13 exit("年龄请输入数字!") 14 15 age_tuixiu = 1 16 if age == 65: 17 age_tuixiu = "您今年退休!" 18 elif age > 65: 19 age_tuixiu = "您该退休啦!" 20 else: 21 age_tuixiu = '您还有'+str(65-age)+'年退休!' 22 23 if salary.isdigit(): 24 salary = int(salary) 25 else: 26 exit("薪资请输入数字!") 27 28 msg2 = "薪资" 29 if salary >= 10000: 30 msg2 = "工资这么高啊!" 31 elif salary >= 4000: 32 msg2 = "工资一般吧!" 33 else: 34 msg2 = "工资也太低了吧!" 35 36 msg = ''' 37 38 ---------- info of %s ---------- 39 姓名:%s 40 年龄:%s 41 工作:%s 42 薪资:%s (%s) 43 %s 44 ---------- end ---------- 45 46 '''%(name,name,age,job,salary,msg2,age_tuixiu) 47 48 print(msg)
输出结果:
%s 字符占位符 string
%d 数字占位符 digit
%f 符点数(相当于小数) float