Login interface and realize guessing number game
- Show welcome message after successful authentication
- Guess the number game, guess three times
- Three-time error after locking
1, adduser.py
1 #The author is tou2 ImportPickle3 #Initialize user and password4User_dict = {"Admin":{"passwd":"Admin","Flag":"Unlock"}}#Dictionary5 6 #Save the account number and password to the file7With open ("Admin.txt","wb+",) as F_write:8 pickle.dump (user_dict,f_write)9With open ("Admin.txt","rb+") as F_read:Tenx =pickle.load (F_read) One Print(x)Add User
2. Log in.py
1 #The author is tou2 ImportPickle3 ImportRandom4 5With open ("Admin.txt","rb+") as F:#Load user account password information6User_dict =pickle.load (f)7Exit_flag = False#the status of the initialization lock is false8Count_lock = 0#Initialize the number of account lockout 0 times9 Ten Print("********* Welcome to login ************". Center (10)) One whileTrue: Auser = input ("Please enter your account number:"). Strip ()#the function of strip () is to remove the first or last space in your input data when you enter your account password. - ifuser = ="":Continue #user name is empty, re-enter -passwd = input ("Please enter your password:"). Strip () the - #determine if there is an account entered - ifuser_dict.get (user): - #determine if the account is locked and the lock exits + ifuser_dict[user]["Flag"] =="Lock": - Print("the account is locked, please unlock it! ") + Break A #Verify that the password is healthy at ifuser_dict[user]["passwd"] ==passwd: - whileTrue: -Guess_choise = input ("Are you going to start playing the number game? (select Yes or no):"). Strip () - ifGuess_choise = ="":Continue - elifGuess_choise = ="Yes": - Print("Welcome to the Python automation development--guessing the digital game system") in Print("******************************************") - Print("guess the numbers range from 1 to 10.") toReal_num = Random.randrange (1,11)#randomly generate a number from 1 to 10 +Retry_count =0 - whileRetry_count<3:#There are 3 chances to guess the numbers . theGuess_num = input ("Please enter the number you guessed:"). Strip () * ifGuess_num = ="":Continue $ ifGuess_num.isdigit ():#the IsDigit () method detects whether a string consists of only numbers. Panax NotoginsengGuess_num =Int (guess_num) - ifGuess_num >Real_num: the Print("Error, please enter a smaller number! ") + elifGuess_num <Real_num: A Print("Error, please enter a larger number! ") the Else : + Print("Congratulations, you guessed your lucky number.%s!!!"%real_num) - Break $ Else: $ Print("input is not a number, please re-enter a number") - Continue -Retry_count +=1 the ifRetry_count = = 3: - Print("three chance runs out, lucky number is%s"%real_num)Wuyi Break the - elifGuess_choise = ="No":#don't want to guess the number game, then exit the system directly Wu Print("Welcome to login again! ") -Exit_flag =True About Break $ Else: - Print("you are not typing yes or no, please re-enter") - Continue - Else: ACount_lock +=1#Incorrect password, statistics of the number of errors + if(3-count_lock): the Print("incorrect account or password, and%s chance to try to login! "% (3-count_lock)) - $ ifCount_lock = = 3:#Lock the account if you have 3 lock times theWith open ("Admin.txt","wb+") as F: theuser_dict["Admin"]["Flag"] ="Lock" #Tag Account admin for lock status theUser_dict = Pickle.dump (user_dict,f)#the modified data is written to Admin.txt . the Print("**************************************************") - Print("the account is locked, please unlock! ") in Break the Else: the Print("the account entered does not exist, please re-enter!") About Continue the the ifExit_flag: the Break + Print("bye!!! ")log in to start the game
Reprint: Article source
Python applet exercise one's Landing interface