码迷,mamicode.com
首页 > 其他好文 > 详细

模拟登陆

时间:2017-05-27 19:59:25      阅读:263      评论:0      收藏:0      [点我收藏+]

标签:账户   src   close   val   while   程序   display   存在   put   

模拟登陆:

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

 

模拟登陆

标签:账户   src   close   val   while   程序   display   存在   put   

原文地址:http://www.cnblogs.com/gzz041/p/6914081.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!