python-Printing Simple company employee Information sheetrequirements, input name is not empty, enter the number of times up to 3 times, 3 times after jumping out of the program;
Knowledge Points:
Raw_input
STR to int
Whil
If elif else continue break
For
Import Module
Reference variable Value
Formatted output
Vim
#!/usr/bin/env pythonimport sysuser_name = "Carson" this_year = 2014counter = 0while 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 () breakage = 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
Enter 3 times to exit the program! # python carson.by please input your name:aa is a not valid user, please try again!please input your name:bb is a not valid user, please try again!please input your name:cc is a not valid user, please try again! Your input 3 times!
Enter a blank character, prompting you to always enter # 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!
Input correct:
Please input your name:carsonwelcome to login system! How old is you?23please input your sex:mdo you hava any hobbies?footballinformation of the company staff Name:carson Age:23 sex:m Hobby:football
Another way to determine the number of inputs is to use a For loop:
#!/usr/bin/env pythonimport sysuser_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 () breakage = 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
python-Printing Simple company employee Information sheet