python-作业1

# Author:Peng Huang
# 作业 编写登录接口
# 需求:1 输入用户密码   2 认证成功之后显示欢迎信息  3 输入3次后锁定
import sys

count = 0
while count < 3:
    usernm = input("please input your username:")     #用户输入用户名
    with open("user_lock.txt", 'r') as f1:             #打开锁定用户文件
        for lou in f1.readlines():                        #逐行循环读取用户锁定文件
            lou = lou.rstrip()                             #去掉换行符
            if usernm == lou:                              #循环对比
                print("your account is being locked!") #匹配成功,打印输出
                sys.exit(0)                                 #跳出循环
    with open('user.txt', 'r') as f2:                    #打开用户密码文件
        for user_list in f2.readlines():                   #逐行循环读取文件内容
            (uname,password) = user_list.strip('\n').split()   #以空格符划分用户和密码信息,并去掉换行符
            if usernm == uname.rstrip():                    #如果输入用户名和文件中用户名匹配
                i = 0
                while i < 3:                               #3次循环
                    passwd = input("please input your password:")
                    if passwd == password:                 #如果密码想同
                        print("welcome to login! %s" % usernm) #打印欢迎信息
                        sys.exit(0)                                #退出循环
                    else:
                        if i != 2:
                            print("your password is not correct,you have %d chance to re-input" % (2 - i))
                    i += 1                                 #输入错误,循环加一
                else:
                    print("your account is being locked!")
                    with open("user_lock.txt", 'a') as loku:
                        loku.write("%s\n" % usernm)  #输入次数超过3次的用户添加到锁定文件中
                        sys.exit(0)
        else:
            if count != 2:
                print("your account is non-existent,you have %d chance to re-input" %(2-count))
        count += 1
else:
    print("your account is non-existent!!!")

猜你喜欢

转载自www.cnblogs.com/William-hp/p/9263223.html
今日推荐