Required. The input name is not empty. It can be entered three times at most and jumps out of the program three times;
Knowledge point:
Raw_input
STR to int
Whil
If Elif else continue break
For
Import Module
Reference variable value
Format 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!
Entered correctly:
please input your name:carsonwelcome to login system!How old are you?23please input your sex:MDo you hava any hobbies?footballInformation of company staff Name :carson Age :23 Sex :M Hobby:football
Another way to judge the number of inputs is to use the 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
This article is from the "Siberian wolf" blog, please be sure to keep this source http://kernal.blog.51cto.com/8136890/1429920