1、占位符 %s
name = input("Name:")age = input("Age:")job = input("Job:")salary = input("Salary:")msg="""-------info of %s-------Name:%sAge:%sJob:%sSalary:%s"""%(name,name,age,job,salary)print(msg) %s,占位符, 后面的%代表与前面的一一对应输出:-------info of ljk-------
Name:ljk
Age:22
Job:IT
Salary:2000 ###
if salary.isdigit(): #判断输入的salary是不是数字,必须输入数字 salary = int(salary)else: print() exit("salary must be digit") 也可以通过占位符来规定必须输入数字: %d ,否则报错:
TypeError: %d format: a number is required, not str
%f 浮点数:小数
时间: 2024-10-13 15:11:48