Python Learning Path-the basic concept (ii)

Source: Internet
Author: User

Before in the blog Park inside wrote an article, address http://www.cnblogs.com/yutianaiqingtian-sky/, but in 51cto inside mix of relatively long, so still want to write in 51cto inside, The study of this python is mainly based on Alex's tutorial, and then hope to continue to learn in a step-by-step way to prepare for admission. This is mainly to complete a job:

Job Two: Write the Login interface

    • Enter User name password

    • Show welcome message after successful authentication

    • Three-time error after locking

According to the idea is to write the flowchart and then write their own algorithm, but actually there are some way out, although the painting flowchart does not draw a lot of time, but the final code is a little way out. Although writing a flowchart to write a program is a guide, but did not develop a flowchart to know how to write a step-by-step code of the ability (5555555555555), the code and flowchart are as follows:

# script name:# function: Write a user login program, if the user input three times after entering the error will lock the user login # author:zhangjinghui#  Date:2016.7.17user_select = int (Input ("Login  or  Register: 1. Login to    2. Registration     3. Exit ")) While user_select == 1 or user_select == 2 or user_ select == 3:    if user_select == 1:         user_name = input ("User name of your account:")          user_password = input ("Your password:")         f =  Open ("User_info.dat",  "R")         f_name = str ( F.readlines (1))                   #将文件中第一行的数据转换为用户易读的形式         f_cnt = str (F.readlines (2 )) &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBsp;cnt = int (f_cnt[8:-4])                  #记录着每次的错误次数         f_password = str ( F.readlines (3))         if f_name[7:-4] != user_name:             print ("The user you entered does not exist ... Please re-enter ... ")         elif cnt >= 3:             print ("You have entered too many errors before, the user has been locked ....")          else:            while  f_password[12:-4] != user_password:                 if cnt >= 3:                      print ("You have entered too many errors, the user has been locked ...")          &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;F_NAME_TEMP&NBSP;=&NBSP;F_NAME[7:-4]                      f_password_temp = f_password[12:-4]                     f.close ()                          #  close the file and open it in write-only mode                      f = open ("User_info.dat",  "W")                      f.write ( ' Name:{0}\ncount:{1}\npass_word:{2}\n '. Format (F_NAME_TEMP,&NBSP;STR (CNT),  f_password_temp))                      f.close ()                      break                 else:                      cnt += 1                     print ("Input error, you also have {} chance". Format (3 - cnt))                      user_password =  input ("Enter your password again:")             if f_ Password[12:-4] == user_password and cnt < 3:                 print ("Successfully logged in ... welcome to login ...")          f.close ()         user_select = int ( Input ("Whether to continue: 1. Login    2. Sign-in    3. Exit"))     elif user_select  == 2:        reg_name = input ("Enter your registered user name:")          reg_password1 = input ("Enter your account password for the 1th time:")          reg_password2 = input ("Enter your account password for the 2nd time:")          if reg_password1 != reg_password2:             print ("Two times input password mismatch ...")         else:             cnt = 0             f = open ("User_info.dat",  "W")              f.write (' name:{0}\ncount:{1}\npass_word:{2}\n '. Format (reg_name, cnt,  REG_PASSWORD1))             f.close ()          user_select = int (Input ("Whether to continue: 1. Log in to    2. Registration     3. Exit "))     elif user_select == 3:         exit ()     else:        print (" Please select the weight 1 or 2 ... ")         user_select= int (Input (" continue: 1. Log in     2. Register    3. Exit "))

650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M01/84/51/wKioL1eMyzHQ-sUDAABr42cyOuE438.png-wh_500x0-wm_3 -wmp_4-s_2300672486.png "title=" Day1 Job 2 flowchart. png "alt=" wkiol1emyzhq-sudaabr42cyoue438.png-wh_50 "/>

Usually write the amount of code is still less, usually look at a very simple thing, but the actual implementation still appeared a lot of forks. According to the requirements of the topic need to do some file output input operation. Code writing there are a lot of shortcomings, but also harvested some, can be summed up as follows: (Refer to Knowledge source http://www.runoob.com/python3/python3-inputoutput.html)

1. Input and output operation

Most of the time in programming is the need to use the print () function to manipulate strings, whereas there are two functions for strings, str () and repr (). There's a slight difference.

    • str (): The function returns a user-readable representation.

    • repr (): produces an interpreter-readable representation

At the same time, more is to adapt to the use of Str.format () format output, it can be considered that the characters in. Format () Replace the position of {} in str, where there is str.rjust (width, fillchar) for the format adjustment, As the name implies, the STR is aligned to the right, adjusted to the width of the length of the character, for the insufficient portion of the content filled with Fillchar. Similar are the Str.ljust () and Str.center (). There are a lot of other, need to continue to learn to learn mining.

2, for file input and output

Now the parts of the contact are slightly simpler to write, pure F.read (), F.write (), F.readline (), F.readlines (). F.read () indicates that all contents of the entire file are extracted directly, and f.readline () reads a row from the file, and F.readlines () extracts the contents of the entire file according to the behavior unit. F.readline () and f.readlines () are often mated to the for line in F.readlines ().

3. Writing code

Write the program to write half to find the compiler to the right of a bunch of horizontal lines (hint written with errors or irregular), the most prompt is that on both sides of the operator should be added space. In the future need to develop a good habit of writing code 650) this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0003.gif "alt=" J_0003.gif "/>


This article is from the "Exclusive Rainy Day" blog, please be sure to keep this source http://520sky.blog.51cto.com/9188727/1827570

Python Learning Path-the basic concept (ii)

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.