Python-day1 homework (thanks to the video teacher homework), python-day1 video

Source: Internet
Author: User

Python-day1 homework (thanks to the video teacher homework), python-day1 video

_ Author _ = 'zht '#! /Usr/bin/env python #-*-coding: UTF-8-*-''' # Try to learn ''' # Number of times counter tries = 0 # define user counter tries_user = 0 # define user lock mark account_flag = 0'' ''while tries <3: # Open the user_list file and the user_lock file. user_list = open('user_list.txt ', 'R') user_lock = open('user_lock.txt', 'R ') # ask the user to enter the user name and password user_name = input ("Enter the User name:") user_pass = input ("enter the password:") # for _ user_lock read the user_lock file cyclically, for _ user_lock in user_lock.r Eadlines (): # _ user_block = _ user_lock loop value. strip () removes spaces between the beginning and end of a row by default. split () with the current row null as the subscript _ user_block = _ user_lock.strip (). split () # if judge if the input username user_name = _ user_block if user_name = _ user_block [0]: # print that the user account has been locked. Print ("this account has been locked") # The user lock mark is equal to 1 account_flag = 1 # the counter is equal to 4, tries = 4 # The break jumps out of the loop and transmits the value where, tries = 4 stop loop break # if Judge user lock mark if it is equal to 0 then execute if account_flag = 0: # for Loop _ user_name read user_list.readlines () user File for _ user_name in user_list.readlines (): # _ user equals _ user_name.strip (). split () removes spaces at the beginning and end of the current row and distinguishes the subscript _ user = _ user_name.strip () with spaces in the current row (). split () # if Judge user input user_name if it is equal to the row in the loop _ user [0] subscript 1, The entered password user_pass is equal to _ user [1] to continue executing Row if user_name = _ user [0] and user_pass = _ user [1]: # indicates that the logon is successful. Print ("Welcome {name} to log on to the Business System Terminal ". format (name = user_name) # print ("welcome to login ..., ", user_name) # The counter is equal to 4 and meets the condition. The break jumps out of the loop and transmits the where value, tries = 4 to stop the loop. tries = 4 break # Or the entered username is correct and the password is incorrect, elif user_name = _ user [0] and user_pass! = _ User [1]: # The system prompts that the password you entered is incorrect. Print ("the user name or password you entered is incorrect! ") # Try adding 1 tries_user + = 1 to the user counter # Jump out of this loop break else: # Otherwise, a message indicating that the user or password you entered is incorrect print ("the user name or password you entered is incorrect! ") # Try the user counter + 1 tries + = 1 # if judgment. if the counter is smaller than 4 if tries <4: # You are prompted to have the remaining chances, 3-remove the number counted by the user. Equal to print ("u have", 3-tries, "chances left! \ N ") # if it is determined that if the number of Hong Lake attempts is equal to 3, if tries_user = 3: # Open the user lock file and append user_lock = open('user_list.txt ', 'A ') # append the user name entered by the user to the locked file and wrap it. User_lock.write (user_name + "\ n") # Close the locked file user_lock.close () # prompt that you have tried multiple times, account and locked print ("lots of atempts, ur account has been locked. ") # Close the User File user_list.close () # Close the user lock file. User_lock.close ()
''' 1. Run the program. First, the program prompts the user to enter the user name and password! You will go to user_lock to check whether the user name entered in the book is correct. If yes, the system will prompt "this account has been locked" if it has not been executed. 2. At this time, the program reads the user_list file to see if there are any users and passwords that match the user input. If yes, log in. If the user name or password is incorrect, the system prompts that the user name or password entered is incorrect. If you do not enter anything or the user name is incorrect, the user name or password is incorrect. 3. the login program will give three chances. If the login user name is incorrect or the password is incorrect, the user will be added to user_lock. The next login will be prompted to have more orders. ''' # Key knowledge involved in this script: ######################################## ######################################## #1. Python strip () the following example shows how to use the strip () function :#! /Usr/bin/python3str = "***** this is string example... wow !!! * *** "Print (str. strip ('*') the output result of the above instance is as follows: this is string example... wow !!! ######################################## ######################################## #2. split () the string is sliced by specifying a delimiter. If the num parameter has a specified value, only the split () method syntax of the num sub-string syntax is separated: str. split (str = "", num = string. count (str )). the str -- delimiter. By default, it contains all null characters, including spaces, line breaks (\ n), and tabs (\ t. Num: the number of splits. Returns the list of split strings. The following examples show how to use the split () function :#! /Usr/bin/python3str = "this is string example... wow !!! "Print (str. split () print (str. split ('I', 1) print (str. split ('W') the output result of the above instance is as follows: ['this', 'is', 'string', 'example .... wow !!! '] ['Th','s is string example... wow !!! '] ['This is string example...', 'O ','!!! '] ###################################### ######################################## ### 3. Open and Close the python file until now, you have learned how to read and write standard input and output. Now we learn how to use the actual data of a file. Python provides basic functions and necessary methods to operate files by default. You can use a file object to perform most file operations. Before reading or writing a file to an open function, you must use the Python built-in open () function to open it. This function creates a file object, which is used to call other support methods related to it. Syntax file object = open (file_name [, access_mode] [, buffering]): file_name: file Name (file_name) the parameter is a string value that contains the file name you want to access. Access_mode: access_mode indicates that the file has been opened, that is, read, write, and append methods. A complete list of possible values, as shown in the following table. This is an optional parameter. The default file access mode is read (r ). Buffering: If the buffer value is set to 0, the buffer is not used. If the buffer value is 1, the row is buffered when a file is accessed. If the specified buffer value is an integer greater than 1, the buffer uses the specified buffer size. If it is a negative number, the buffer size is the default value (default behavior ). Here is to open a list of different file modes-mode description r to open a file as read-only. The file pointer is placed at the beginning of the file. This is the default mode. A rb 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 + open the file used to read and write binary data. The file pointer is placed at the beginning of the file. W open a file and write only. If the file overwrites the file. If the file does not exist, a new file is created and written. A wb file can only be written in binary format. If the file overwrites the file. If the file does not exist, a new file is created and written. W + opens the file in the write and read modes. If the file exists, overwrite the existing file. If the file does not exist, create a new file for read/write operations. Wb + enables writing and reading files in binary format. If the file exists, overwrite the existing file. If the file does not exist, create a new file for read/write operations. A. open the file to be appended. The file pointer is at the end of the file. That is, the file is in append mode. If the file does not exist, it will create a new file for writing. The AB open file is used to append the file in binary format. The file pointer is at the end of the file. That is to say, the file is in the append mode. If the file does not exist, it will create a new file for writing. A + the method for opening an object is append and read. The file pointer is at the end of the file. This file is opened in append mode. If the file does not exist, it will create a new file for read/write operations. AB + opens a file in the additional and binary format read mode. If the file has a file pointer at the end of the file. This file is opened in append mode. If the file does not exist, it will create a new file for read/write operations. Once the file object attribute is opened, a file object is generated and you can obtain various information about the file. Here is a list of all attributes related to a file object: attribute description file. closed returns true if the file is closed; otherwise, it is falsefile. mode returned file open access mode file. name returned file name Note: Soft Space attributes do not support examples in Python3.x #! /Usr/bin/python3 # Open a filefo = open ("foo.txt", "wb") print ("Name of the file:", fo. name) # Close opened filefo. close () ######################################## ######################################## #4. Another format is defined as user_name = input ("Enter the User Name: ") for output, you can use {here you can enter it casually}. The input in the brackets at the end is equal to the variable name above. Here, there is a dot in front of the format.. Format (name = user_name) Example: print ("Welcome {name} to log on to the Business System Terminal". format (name = user_name ))'''

  

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.