python-简单用户交互小程序
1、第一种方式:
#!/usr/bin/python
#--*-- coding:utf-8 --*--Name = raw_input('please your name:\n')
Age = raw_input('please your age:\n')
Sex = raw_input('please your sex:\n')
Job = raw_input('please your job:\n')
print '---------------------------------------'
print 'Information of company staff:\n'
print ' Name:',Name
print ' Age: ',Age
print ' Sex:',Sex
print ' Job:',Job
[email protected]:~/python# python userinput.py
please your name:
xuweiyy
please your age:
23
please your sex:
male
please your job:
securitior
---------------------------------------
Information of company staff:
Name: xuweiyy
Age: 23
Sex: male
Job: securitior
2、第二种方式:使用'''............'''
[email protected]:~/python# vim userinput.py
#!/usr/bin/python
#--*-- coding:utf-8 --*--
name = raw_input('Name:')
age = raw_input('Age:')
sex = raw_input('Sex:')
job = raw_input('Job:')
print '------------------------------\n'
#print '\tName:',name,'\n\tAge:',age,'\n\tSex:',sex,'\n\tJob:',job
print '''\tName: %s
\tAge: %s
\tSex: %s
\tJob: %s''' % (name,age,sex,job)
~
[email protected]:~/python# python userinput.py
Name:xuweitt
Age:23
Sex:male
Job:workor
------------------------------
Name: xuweitt
Age: 23
Sex: male
Job: workor
[email protected]:~/python#
3、在raw_input('')输入特定类型
[email protected]:~/python# vim userinput.py
#!/usr/bin/python
#--*-- coding:utf-8 --*--
name = raw_input('Name:')
age = int(raw_input('Age:')) //输入特定的整型数字类型
sex = raw_input('Sex:')
job = raw_input('Job:')
print '------------------------------\n'
#print '\tName:',name,'\n\tAge:',age,'\n\tSex:',sex,'\n\tJob:',job
print '''\tName: %s
\tAge: %s
\tSex: %s
\tJob: %s''' % (name,age,sex,job)
[email protected]:~/python# python userinput.py
Name:www
Age:dd
Traceback (most recent call last):
File "userinput.py", line 4, in <module>
age = int(raw_input('Age:'))
ValueError: invalid literal for int() with base 10: 'dd'
[email protected]:~/python# python userinput.py
Name:www
Age:23
Sex:33
Job:44r
------------------------------
Name: www
Age: 23
Sex: 33
Job: 44r
[email protected]:~/python#