Recently did a few exercises, with some of the basic knowledge of Python, but each problem has a level, need a solid foundation.
First Way
Basic requirements: Let the user enter the user name password Authentication successful display welcome information after the error three to exit the program upgrade requirements: can support multiple users login (prompt, through the list to save multiple account information) Users 3 times authentication failed, quit the program, restart the program when you try to log on, or lock the status (prompt: Need to save user-locked state to file)
Knowledge: List operations, file read and write operations, loops, conditional branches, functions, etc.
The code is as follows:
1 defLogin (user):2Count =03 whileCount<3:4Username = input ('Username:')5Password = input ('Password:')6 forKinchUser:7 ifUSERNAME==K[0] andPassword==k[1]:8 Print('Welcome%s!'%(K[0]))9 return0Ten Print('the user name or password you entered is incorrect! \ n') OneCount +=1 AFile.write ('0') - file.close () - theFile=open ('Login.txt','r+') -user_information=[['Alex','abc123'],['Jack','123456'],[' Pay','654321']] -Data=File.read () - ifdata=='1': + Login (user_information) - Else: + Print('your account has been locked! ') AFile.close ()
The basic functionality has been implemented, but there are several issues:
1. The data entered by the user is not processed by the leading and trailing blanks;
2. Because it is the first time to formally write Python code, but also should pay more attention to the code writing specifications, see PEP8 specification;
3. Add less comments to the code;
4. The need to understand the deviation, business logic is biased, the current user is locked, other users should also be able to log in.
Although the problem is simple, but there are still a lot of problems, should pay attention to correct!
Basic syntax, data type, character encoding, file manipulation (i) for Python practice