python作业---实现登陆接口

# -*- coding: utf-8 -*-
#   Author: Jason Wang

import sys
retry_limit = 3
retry_count = 0
account_file = 'accounts.txt'
lock_file = 'account_lock.txt'
global userName

while retry_count < retry_limit:  # 重复次数没超过3次,就不断循环

    userName = input('Please enter userName:')
    # 用户输入用户名之后,打开account_lock.txt文件,以检查该用户是否已经被lock了
    lock_check = open(lock_file)

    for line in lock_check.readlines():  # 循环account_lock.txt文件
        if userName == line.strip():  # 如果该用户已经被lock了,就直接退出
            sys.exit('User %s  has been locked!' % userName)

    passWord = input('Please input passWord:')

    f = open(account_file, "r+")  # 打开账号文件
    match_flag = False    # 初始值为False,如果用户match成功了,就设置为True
    for line in f.readlines():
        '''''去掉每一行末尾的\n,并把这一行以空格为分隔符进行分割,
           将分割出来的用户名和密码分别赋值给user和pwd两个变量
        '''
        user, pwd = line.strip().split(":")
        if user == userName and pwd == passWord:  # 判断用户名和密码是否同时匹配
            print(userName, 'has match successfully!')
            match_flag = True
            break  # 匹配成功就跳出for循环
    f.close()
    if not match_flag:  # if match_flag == false
        '''如果match_flag为False,表示上面的for循环中没有match上用户名和密码
        '''
        print('用户名或密码错误!')
        retry_count += 1
    else:
        print('Welcome login Python Learning system!')
        break  # 用户登录成功,打断while循环
else:
    print('Your account has been locked!')
    f = open(lock_file, 'a+')
    f.write(userName+'\n')      # 将用户信息写入锁用户文件,包括换行符
    f.close()

 

posted @ 2017-05-24 15:54  jason_dd  阅读(160)  评论(0)    收藏  举报