User login interface program

Entitled: Login to write interfaces

Requirements: 1, enter your user name and password;

           2, after successful authentication display a welcome message

           3, after unsuccessful attempts to lock;

The subject of the request is first made the following flow chart:

2, the initial contact python, many details still unfamiliar knowledge, prepared to problems encountered during their own programming, and solutions to record, do accumulate for future learning, but also provide experience for other python beginner learners.

1) programming process, there was an error message: "unexpected indent".

      This error occurs because: input statement, and more indented four grid.

2, the programming process uses, open the function txt document.

Question 1: how to achieve open txt document?

lock_file = open ( "txt file path")

lock_list=lock_file.readlines()

print(lock_list)

To achieve the basic function of open txt document;

Question 2: When txt documents have strings and numbers, open the txt file,

lock_file = open ("D:\python\studydiary\oneweek\user.txt")

An error message: (unicode error) 'unicodeescape' codec can not decode bytes in position 28-29: truncated \ uXXXX.

This is because, in python "\" represents the escape character.

The solution is: 1) in front of the string plus a "r" on it.

= Open LOCK_FILE (R & lt "D: \ Python \ studydiary \ oneweek \ user.txt ') 
2) in the" \ "front, plus a" \ "escape, lock_file = open (" D: \\ python \\ studydiary \\ oneweek \\ user.txt ")

 Question 3: txt document is stored in "

zhanger
lili
baidu

Read out by the above procedure the result is:

[ 'Zhanger \ n', 'lili \ n', 'baidu \ n'] "

I do not want "\ n" how to solve?

Solution: Use strip () function to remove the end of each line \ n

strip () Function Prototype

Disclaimer: s a string, rm is a sequence of characters to be deleted

s.strip (rm) to delete the beginning of the string s, at the end, located in the sequence of characters to delete rm

E.g:

 for line in file.readlines():

      line=line.strip('\n');

In the present embodiment the resolution code is as follows:

= Open LOCK_FILE ( "D: \\ Python oneweek \\ \\ \\ studydiary lockeduser.txt") 
lock_list lock_file.readlines = ()
for lock_line in lock_list:
lock_line = lock_line.strip ( '\ n-')
Print (lock_line)
after running the program, the output results are as follows:

3, to achieve abnormal, the abnormal exit to function.

Two ways to exit the program in python, sys.exit and os.exit (0), 0 is the default state, can be empty, both of which will exit the program currently running. os.exit of 0 can not be omitted.

1) sys.exit (0): can capture systemexit abnormal, then do the appropriate clean-up work, the results are output directly goodbye.

2) os.exit (0): direct interrupt and exit the program immediately, no matter what.

In this procedure, it is required, when the user database query to the lock to lock the user, is displayed, the user changes the user name is locked, and abnormal exit, so use the sys.exit ()

4, reads the document text from txt, is of the form

'Zhangsan 123' 'lisi 234' 'zhangping 345'

How to achieve a name and password separately extracted and stored?

python, treated with split () function to achieve the cutting function strings. split () syntax structure is as follows:

str.split(str="",num=string.count(str))

E.g:

str="I come from Beijing"

print (str.split ()) # spaces divided as a delimiter

print (str.split ( 'o', 1)) # o to the first separator to be divided

Divided print (str.split ( 'm')) # m as a separator to

Examples of the above output results are as follows:

[‘I','come','from','Beijing']

  ['I c','me from Beijing']

  ['I co','e fro','Beijing']

5, the performance function code is as follows:

import sys,getpass,io

i=0
while i<3:
    username = input("username:")
    locked_user_information = open(r"//Users//yxy-zhaolu//Documents//Share Data//9//locked_user_information.txt")
    locked_user_list = locked_user_information.readlines()
    user_information = open("//Users//yxy-zhaolu//Documents//Share Data//9//user_information.txt")
    user_information_list = user_information.readlines()

    for locked_user_line in locked_user_list:   # open a locked file search and user name if the user to lock the user 
        locked_user_line locked_user_line.strip = ( ' \ the n- ' )
         IF username == locked_user_line:
             Print ( " ! Sorry, the this username HAD Locked " )
            sys.exit()

    for user_information_line in user_information_list:
        (user_name,pass_word) = user_information_line.strip('\n').split()
        if username == user_name:
            j=0
            while j<3:
                password = input("password:")
                if password == pass_word:
                    print("welcome you, ",username)
                    sys.exit()
                else:
                    if j<3:
                        print("sorry,you have wrong password,please try again")
                j=j+1
            else:
                add = open("//Users//yxy-zhaolu//Documents//Share Data//9//locked_user_information.txt",'w')
                locked_user_information=add.write(username)
                locked_user_information.close()
                sys.exit
        else:
            print("this username is not exist")
            pass
    i=i+1
else:
    sys.exit()
locked_user_information.close()
user_information.close()

 

 

 

Guess you like

Origin www.cnblogs.com/zhaolu/p/11087475.html