模拟登陆

模拟登陆:

1. 用户输入帐号密码进行登陆

2. 用户信息保存在文件内

3. 用户密码输入错误三次后锁定用户

 

def user_list():  # 打印用户名函数
    for k in user_dict:
        print(k)

with open('user_dict','r',encoding='utf-8') as f:
    for line in f:
        user_dict = eval(line.strip())

    with open('locked','r+',encoding='utf-8') as l:
        for line2 in l:
            lock_str = line2

locked = lock_str.split('|')

while True:
    user_list()
    print("选择登陆系统的用户名")
    enter_name = input("输入用户名:").strip()
    count = 0
    if enter_name in locked:
        print("账户{0}已锁".format(enter_name))
        break
    if enter_name in user_dict:
        while count < 3:
            enter_passwd = input("输入用户密码").strip()
            if enter_passwd == user_dict[enter_name]:
                print("登录成功!")
                break
            else:
                count += 1
                print("密码错!")
                try_times = 3 - count
                print("您用的登录名{0}登录密码错误,您还剩{1}次机会".format(enter_name, try_times))
                print("您用的登录名{0}已锁".format(enter_name))
        else:
            lock_str = lock_str+'|'+enter_name
            # temp_list.append(enter_name)  # todo how to write to the list
            # temp = str(locked)
            # l.write(temp)
            # l.flush()
            with open('locked', 'r+', encoding='utf-8') as x:
                x.write(lock_str)
                x.flush()
            break
    if len(enter_name) == 0 or enter_name not in user_dict:continue

'''
写在后面:
所有程序开始先把事情按照最好的流程撸一遍,
再考虑突发的事情
最后考虑是否要加循环
'''
View Code

 

posted @ 2017-05-27 18:25  gzz041  阅读(142)  评论(0编辑  收藏  举报