One:
Writing the Login interface
Basic requirements:
Let the user enter the user name password
Show welcome message after successful authentication
Exit the program after a three-time error
1.dic method
name_info={'Zhang':'123'}count=0 whileCount<3: Name=input ('Please enter user name:') ifNameinchName_info:password=input ('Please enter your password:') ifPassword = =Name_info[name]:Print('Welcome Log in') Break Else: Print('password entered incorrectly, please re-enter the password:') Count+=1Else: Print('input information is incorrect, please re-enter') Count+=1
2.list method
name_info=['Zhang','123']count=0 whileCount < 3: Name=input ('Please enter user name:') ifName = =name_info[0]: password=input ('Please enter your password:') ifPassword = = Name_info[1]: Print('Welcome Log in') Break Else: Print('Password input error, please re-enter') Count+=1Else: Print('User name input error, please re-enter') Count+=1
Second: The requirements of the upgraded version:
Can support multiple users login (prompt, through the list to save multiple account information)
After the user 3 authentication failed, quit the program, start the program again when attempting to log on, or lock the status (hint: the user locked state must be stored in the file)
name_info={'Zhang':{'passwd':'123','Count': 0},'Xin':{'passwd':'123','Count': 0},'Xiao':{'passwd':'123','Count': 0}} Count=0 whileTrue:name=input ('Please enter your user name:') ifName not inchName_info:Print('This user name does not exist, please re-enter! ') ContinueWith Open ('Db.txt','R') as F:lock_users=f.read (). Split ('|') ifNameinchlock_users:Print('user%s has been locked'%name) Break ifname_info[name]['Count'] > 2: Print('too many attempts, locked') with open ('Db.txt','a') as F:f.write ('%s|'%name) Break #if name in Name_info:Password=input ('Please enter your password:') ifPassword = = name_info[name]['passwd']: Print('Log In Success') Break Else: Print('Password input error, please re-enter:') name_info[name]['Count']+=1
Python Basic Login Interface