用户登录

使用Python写的用户登录程序

#!/usr/bin/env python
# _*_coding:utf-8_*_
# date:2016-7-27

import time
f = open("data")  # 打开文件
lines = f.readlines()    # 读取文件
num = len(lines)         # 列表内的元素个数
count = 0
error = 0
while count == 0:
    user_input = input("please input the user(q or Q exit login):")
    if user_input.lower() == 'q':
        break
    pass_input = input("please input the password:")

    for i in range(num):

        line1 = lines[i]     # 列表的元素赋给line1

        line1_new = line1.strip('\n')  # 去除列表里每一个元素的结尾\n
        line_new = line1_new.split(':')  # 以:为分割线进行字符串的分割使用户名和密码分开

        if line_new[0] == user_input and line_new[1] == pass_input:  # 如果用户名和密码匹配就登录成功
            print('''---------------------------
        登录成功
---------------------------''')
            count = 1
            break

        # 如果用户名和密码只匹配上一项就提醒用户检查自己的输入
        elif line_new[0] == user_input and line_new[1] != pass_input:
            print("check your password ")
            error += 1

            break
        # 如果最后一个都没有匹配到就输出没有此用户
        while i == num-1 and line_new[0] != user_input:
            print('no user')
            i = -1
    while error == 3:     # 错误3次直接锁定
        print('you are locked 3 minutes')
        time.sleep(180)    # 锁定3分钟
        error = 4

f.close()
posted @ 2016-07-27 16:20  Dus  阅读(174)  评论(0编辑  收藏  举报