Basic syntax, data type, character encoding, file manipulation (i) for Python practice

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.