python-用户交互程序
简单的用户输入:
1 username = input("username:") 2 password = input("password:") 3 print(username,password)
输出:
username:kami
password:kami
kami kami
低端格式化输出(字符串拼接):
1 #Author:Archer Zon 2 name = input("name:") 3 age = input("age:") 4 job = input("job:") 5 salary = input("salary:") 6 7 info = ''' 8 ----info of '''+name+'''----- 9 Name:'''+name+''' 10 Age:'''+age+''' 11 Job:'''+job+''' 12 Salary:'''+salary+''' 13 ''' 14 print(info)
中端格式化输出:
1 name = input("name:") 2 age = input("age:") 3 job = input("job:") 4 salary = input("salary:") 5 6 info = ''' 7 ----info of %s----- 8 Name:%s 9 Age:%s 10 Job:%s 11 Salary:%s 12 '''%(name,name,age,job,salary) 13 print(info)
默认所有的输入都是字符串
将age改为整数型输入:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 name = input("name:") 2 age = int(input("age:")) 3 print(type(age)) 4 job = input("job:") 5 salary = input("salary:") 6 7 info = ''' 8 ----info of %s----- 9 Name:%s 10 Age:%d 11 Job:%s 12 Salary:%s 13 '''%(name,name,age,job,salary) 14 print(info)
python 2.x里面的raw_input和python 3.x里面的input功能一样
高级格式化输出:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 name = input("name:") 2 age = int(input("age:")) 3 print(type(age)) 4 job = input("job:") 5 salary = input("salary:") 6 7 info = ''' 8 ----info of {_name}----- 9 Name:{_name} 10 Age:{_age} 11 Job:{_job} 12 Salary:{_salary} 13 '''.format(_name = name,_age = age,_job = job,_salary = salary) 14 print(info)