1. Guess The Age
User_lisa = # defines an age count = 0 # loop Count while Count < 3: # Cycles less than 3 times user = input (' Please your enter ' Rname: '). Strip () # User input, go to both spaces if user.isdigit (): # If the input is a numeric string, the user = Int (user) # is converted to an integer type if user = = User_lisa: # Users enter a value equal to the defined value, exit the entire program print (' You got it ') exit () elif user > user_lisa:< c14/># user input value is greater than the defined value, prompt to lose dot print (' try smaller ') else: user < User_lisa # users enter a value less than the defined value, prompted to lose a large point print (' Try Bigger ') else: print (' Try Again ') # If user input is not a number, prompt retry count + = 1 # Number of cycles added 1print (' Too many times ') # Too many tips
2. User Login
Method One:
Count = 0user_name = ' Lisa ' pwd = ' 123 ' While Count <3: username =input (' Please enter your username: ') password = Input (' Please enter your password: ') if username = = User_name and Password = = pwd: While True: user_cmd = Inpu T (' Please enter your cmd: '). Strip () print (' The cmd is%s '%user_cmd ') if user_cmd = = ' Q ': Break break Else: print (' Login failed ') count + = 1
Method Two:
Count = 0user_name = ' Lisa ' pwd = ' 123 ' While Count <3: username =input (' Please enter your username: ') password = Input (' Please enter your password: ') if username = = User_name and Password = = pwd: While True: user_cmd = Inpu T (' Please enter your cmd: '). Strip () print (' The cmd is%s '%user_cmd) if user_cmd = = ' Q ': exit () else: print (' Login failed ') count + = 1
3, use while loop output 1, 2, 3, 4, 5, 6, 8, 9, ten
# use while loop output 1, 2, 3, 4, 5, 6, 8, 9, 10count = 1 # to define the initial value while count < one: # when Count is less than 11 o'clock, execute the following code if Count = = 7:
# when Count equals 7 o'clock, Count is 1, and the loop is returned by count + = 1 continue print (count) # Prints the value of count + = 1 # count self plus 1
Output Result:
1234568910
View Code
Pytthon Foundation first day--if judgment, while loop, for loop, Python string