Python-day1 homework (Thanks for the video teacher's homework)

Source: Internet
Author: User

__author__ = ' zht ' #!/usr/bin/env python#-*-coding:utf-8-*-' #努力学习每一天 ' #尝试次数计数器tries = 0# definition Try user counter tries_user = 0# definition with User lock Tag Account_flag = 0 ' first layer loop where counter is less than 3 execute ' while tries <3: #打开user_list文件和user_lock文件, user_list = open (' User _list.txt ', ' r ') User_lock = open (' User_lock.txt ', ' r ') #要求用户输入用户名和密码 user_name = input ("Please enter user name:") User_pass = i Nput ("Enter password:") #for _user_lock Loop read User_lock file, for _user_lock in User_lock.readlines (): # _user_block= _user The value of the _lock loop. Strip () The default strip line and row space split () divides the current line space with the subscript _user_block = _user_lock.strip (). Split () #if判断 if the user name you entered User_n            Ame=_user_block if user_name = = _user_block[0]: #打印这个用户账户已经被锁定. Print ("This account has been locked") #用户锁定标记等于1 Account_flag = 1 #计数器等于4, tries = 4 # Break jumps out of loop, value where,tries = 4 Stop loop break #if判断用户锁定标记如果等于0就往下执行 if Account_flag = = 0: #for循环 _user_na Me read user_list.readlines () user file for _user_name in User_list.readlines (): #_user等于_user_name. Strip (). Split () loops out the current line and strips the white space from the end of the current line, and distinguishes the subscript _user = _use R_name.strip (). Split () #if判断用户输入user_name如果等于循环出的行_user [0] Subscript 1, enter the password user_pass and equals _user[1] continue to execute if user                _name = = _user[0] and User_pass = = _user[1]: #提示登录成功. Print ("Welcome {name} login to Business System terminal". Format (name=user_name)) # print ("Welcome to login ...,", user_name) #            Counter equals 4 qualifying, break jumps out of loop, value where,tries = 4 Stop loop tries = 4 break #或者 The user name entered is correct, and the password is incorrect,                elif user_name = = _user[0] and User_pass! = _user[1]: #提示您输入的密码错误. Print ("The user name or password you entered is wrong!")            ") #尝试用户计数器加1 Tries_user +=1 #跳出本次循环 Break Else: #否则不符合以上条件的提示您输入的用户或者密码错误 print ("The user name or password you entered is wrong! ") #尝试用户计数器 +1 tries +=1 #if判断 if the counter is less than 4 if tries < 4: #提示你还剩下几次机会, 3-The number of user counts to goWord.            equals the result print ("U has", 3-tries, "chances left!\n") #if判断如果洪湖尝试次数等于3, if Tries_user = = 3:            #打开用户锁定文件, Append user_lock = open (' User_list.txt ', ' a ') #把用户输入的用户名追加到锁定文件并且换行. User_lock.write (user_name+ "\ n") #关闭锁定文件 User_lock.close () #提示你已经尝试多次, account and locked P Rint ("Lots of atempts, ur account had been locked.") #关闭用户文件user_list. Close () #关闭用户锁定文件. User_lock.close ()
"1, run the program, the first program will prompt the user to enter the user name and password, after input! Will go to User_lock to see if there is a user name just entered in the book, if there is, will prompt "This account has been locked" if not continue to execute. 2, this time, the program execution read user_list This file, see if there is no user input and password matching. If there is, welcome to login, if the user name or password is not correct, prompting the user to enter the user name or password error. If you do not enter anything, or if the user name is incorrect, the user name or password error is also prompted. 3, the landing process will give 3 opportunities. If the login username is incorrect or the password is incorrect, the user will be added to the user_lock. The next landing will be prompted to order more. "" #本脚本涉及的几个关键知识: ################################################################################ #1, Python The Strip () method removes the character specified by the string (the default is a space) The following example shows how the strip () function is used: #!/usr/bin/python3str = "*****this is a string EXAMPLE....WOW!!! "Print (Str.strip (')") the output of the above instance is as follows: this is string EXAMPLE....WOW!!! ################################################################################ #2, Split () slices a string by specifying a delimiter, If the parameter num has a specified value, then only the NUM substring syntax split () method syntax is delimited: str.split (str= "", Num=string.count (str)). parameter str--delimiter, default to all null characters, including spaces, line breaks (\ n) , tabs (\ t), and so on. Num--the number of splits. The return value returns the segmented list of strings. Example The following example shows how the split () function is used: #!/usr/bin/python3str = "This is string EXAMPLE....WOW!!!" Print (Str.split ()) Print (Str.split (' I ', 1)) Print (Str.split (' W ')) The output of the above example is as follows: [' This ', ' is ', ' String ', ' EXAMPLE....WOW!!! '] [' th ', ' s is string EXAMPLE....WOW!!! '] [' This is string example .... ', ' o ', '!!! '] ################################################################################ #3, Python file open open and close files so far, Read and write standard inputs and outputs have been learned. Now, let's learn how to use the actual file data. Python provides the basic functionality and methods necessary for the default action file. You can use a file object to do most of the document operations. The open function must be opened by using the Python built-in open () function before reading or writing to a file. The function creates a file object, which is used to invoke other support methods associated with it. Syntax File object = open (file_name [, access_mode][, Buffering]) The following is the details of the parameter: the file_name: File name (file_name) parameter is a string value that contains the file name that you want to access. ACCESS_MODE:ACCESS_MODE specifies that the file has been opened, that is, read, write, append, and so on. A complete list of possible values, as shown in the table below. This is an optional parameter, and the default file access mode is read (R). Buffering: If the buffer value is set to 0, it means that no buffering is used. If the buffer value is 1, a file is accessed for the time buffer. If you specify an integer that has a buffer value greater than 1, the buffer is buffered using the indicated buffer size. If it is negative, the buffer size is the system default (the default behavior). Here is a list of different modes of opening a file-mode description R opens a file for read-only. The file pointer is placed at the beginning of the file. This is the default mode. RB open a file can only be read in binary format. The file pointer is placed at the beginning of the file. This is the default mode. R+ open for reading and writing files. The file pointer is placed at the beginning of the file. rb+ opens a file for reading and writing binary formats. The file pointer is placed at the beginning of the file. W open a file to write only. Overwrite the file if it exists. If the file does not exist, it is created to write to the new file. WB open a file can only be written in binary format. Overwrite the file if it exists. If the file does not exist, it is created to write to the new file. w+ open file for write and read mode. Overwrite existing file if file exists. If the file does not exist, create a new file for read-write operations. wb+ Open for binary gridWrite and read-out files. Overwrite existing file if file exists. If the file does not exist, create a new file for read-write operations. A opens the file for appending. The file pointer is the end of the file if the file exists. In other words, the file is in append mode. If the file does not exist, it creates a new file to write to. AB Open file is used for appending in binary format. The file pointer is the end of the file if the file exists. In other words, the file is in append mode. If the file does not exist, it creates a new file to write to. A + Opens the file for append and read mode. The file pointer is the end of the file if the file exists. The file opens in Append mode. If the file does not exist, it will create a new file for read and write operations. ab+ opens a file in append and binary format read mode. If the file exists, the file pointer is at the end of the file. The file opens in Append mode. If the file does not exist, it will create a new file for read-write operations. File object Properties Once the files are opened, there will be a file object so you can get various information about the file. Here is a list of all the properties related to a File object: Property Description file.closed If the file is closed returns TRUE, otherwise Falsefile.mode returns the file open access mode file.name return filename Note: Soft Space properties are not supported in python3.x sample #!/usr/bin/python3# Open a filefo = open ("Foo.txt", "WB") Print ("Name of the file:", Fo.name) # Close opened Filefo.close ( ################################################################################ #4, there is a format such as defining such a variable user_ Name = input ("Please enter user name:") output can be used {This can be casually entered} The last square in the parentheses entered to be equal to the previous variable name. There is a dot in front of format here.: Format (name=user_name) example, print ("Welcome {name} login to Business System terminal". Format (name=user_name)) "

  

Python-day1 homework (Thanks for the video teacher's homework)

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.