First Python program-determine if login username and password are correct

Source: Internet
Author: User

#setencoding =utf-8#用户名和密码输入正确, the login success # user name correct password error, only enter the password, there are 3 chance # error 3 times, then put the user name in lock import os,sys# the file that holds the user name and password is E:\python\ User.txtaccounts_file='E:\\python\\user.txt'#被锁的用户要写入此文件lock_file='E:\\python\\lock.txt'#先读出来放内存中, so that every time you enter the user after the decision to open file user=file (accounts_file) account_list=user.readlines () user.close () #控制是否退出while大循环的标识loginSucess=false# here with the large while loop, so the user name is not lost, will always lose, if you want to limit the number of user name input, then the while change to for whileTrue: #raw_input是一个内置函数, get user input, interact with user #strip () is Ignore space username= Raw_input ('Username:'). Strip () #先判断用户名是否存在, if the user name exists before continuing, if it does not exist, jump out of the loop directly, endifLen (username)! =0: #用户名如果不为空的时候, judging whether there is forIinchaccount_list: #i读到的是一行, and a row has 2 columns, the 1th column is the user name with index 0, the second column index is the password #split () method is the delimiter, the default is a space, I 
    =I.split ()ifusername==i[0]: #用户名正确 #用户名正确的时候, give only 3 times the opportunity to enter the password forXinchRange3): Password= Raw_input ('Password:'). Strip ()ifpassword==i[1]: loginsucess=true# If true, go straight to the if of the superior for, exit when you're done . Break#如果密码错误 #如果匹配正确或循环中有break就退出, else do not executeElse: Print'%s, you have entered the wrong password 3 times, going to lock'%username L= File (Lock_file,'a') L.write (username+'\ n') L.close () View=file (lock_file) print view.read ()ifLoginsucess isTrue:print'Login successful, Welcome to my system!!!'             Break#登陆成功就直接跳出while大循环 elif loginsucess isFalse: #这种是用户名输入错误, its node under the condition that the user is not empty, here cannot break, cannot jump out while cycle, because I want you to lose until successfully print'User name input error, please re-enter. '        Else: #用户名也正确, enter the password incorrectly 3 times print'%s,sorry, enter the password 3 times error you have been locked'%username Break    Else: #用户名输入为空的时候Continue

First Python program-determine if login username and password are correct

Related Article

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.