python-打印简单公司员工信息表
要求,输入name不为空,输入次数最多3次,3次后跳出程序;
知识点:
raw_input
str转int
whil
if elif else continue break
for
导入模块
引用变量值
格式化输出
vim
#!/usr/bin/env python
import sys
user_name = "carson"
this_year = 2014
counter = 0
while True:
if counter < 3:
name = raw_input("please input your name:").strip()
if len(name) == 0:
print "empty name , please input your name again!"
continue
elif name == user_name:
pass
print "welcome to login system!"
else:
print "%s is a not valid user, please try again!" % name
counter = counter + 1
continue
break
else:
print "Your input 3 times!"
sys.exit()
break
age = int(raw_input("How old are you?"))
sex = raw_input("please input your sex:")
hobby = raw_input("Do you hava any hobbies?")
information = '''Information of company staff
Name :%s
Age :%d
Sex :%s
Hobby:%s
''' % (name,age,sex,hobby)
print information
输入3次,退出程序!
# python carson.by
please input your name:a
a is a not valid user, please try again!
please input your name:b
b is a not valid user, please try again!
please input your name:c
c is a not valid user, please try again!
Your input 3 times!
输入空白字符,提示一直输入
# python carson.by
please input your name:
empty name , please input your name again!
please input your name:
empty name , please input your name again!
please input your name:
empty name , please input your name again!
please input your name:
empty name , please input your name again!
please input your name:
empty name , please input your name again!
please input your name:
empty name , please input your name again!
please input your name:
empty name , please input your name again!
please input your name:
empty name , please input your name again!
please input your name:
empty name , please input your name again!
输入正确:
please input your name:carson
welcome to login system!
How old are you?23
please input your sex:M
Do you hava any hobbies?football
Information of company staff
Name :carson
Age :23
Sex :M
Hobby:football
在判断输入次数的还有一种方法是利用for循环:
#!/usr/bin/env python
import sys
user_name = "carson" while True:
name = raw_input("please input your name:").strip()
if len(name) == 0:
print "empty name , please input your name again!"
continue
for i range(1,3):
name = raw_input("please input your name:").strip()
if name == user_name:
pass
print "welcome to login system!"
else:
print "%s is a not valid user, please try again!" % name
continue
break
else:
print "Your input 3 times!"
sys.exit()
break
age = int(raw_input("How old are you?"))
sex = raw_input("please input your sex:")
hobby = raw_input("Do you hava any hobbies?")
information = '''Information of company staff
Name :%s
Age :%d
Sex :%s
Hobby:%s
''' % (name,age,sex,hobby)
print information