登陆程序作业

编写登陆接口:

1.输入用户名密码

2.认证成功后显示欢迎

3.输错三次后锁定

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author:Mgin
count = 0
username = input('username:')
with open('locked.txt','r',encoding='utf-8') as f:
    for line in f:
        user_locked = line.strip().split()
        if username == user_locked[0]:
            print("User {name} have been locked".format(name=username))
            f.close()
            exit()

with open('user.txt','r',encoding='utf-8') as d:
    for line in d:
        username_data = line.strip().split()[0]
        if username == username_data:
            password = input('password:')
            password_data = line.strip().split()[1]
            if password == password_data:
                print("Welcome user {name} login...".format(name=username))
                d.close()
                break
            elif password != password_data:
                for i in range(2):
                    print("Invalid username or password!")
                    count += 1
                    password = input('password:')
                    if password == password_data:
                        print("Welcome user {name} login...".format(name=username))
                        d.close()
                        exit()
                    elif count == 2 :
                        print("User {name} have been locked".format(name=username))
                        with open('locked.txt','a+',encoding='utf-8') as e:
                            e.write('%s\n'%username)
                            e.close()
                            exit()
    else:
        print("User {name} was not found...".format(name=username))
        exit()
user.txt
aaa 111
bbb 222
locked.txt
ccc

 

posted @ 2017-08-10 22:33  Mgin  阅读(174)  评论(0)    收藏  举报