• python_day1_作业1

    要求:

    输入用户名密码
    认证成功后显示欢迎信息
    输错三次后锁定

     

    粗略画了张进程图:

     

    代码如下:

    i = 0
    while i < 3:
        name = input("请输入你的账号:")
        passwd = input("请输入你的密码:")
    
        lock_file = open("account_lock.txt","r+")
        lock_file_line = lock_file.readlines()
        for lock_user in lock_file_line:
            if name in lock_user:
                exit("账号%s已经被锁定"%name)
    
        user_pw_file = open("account.txt","r+")
        user_pw_file_line = user_pw_file.readlines()
        for user_pw_list in user_pw_file_line:
            user_password = user_pw_list.strip().split("|")
            if name == user_password[0]:
                j = 0
                while j < 3:
                    if passwd == user_password[1]:
                        print("欢淫%s归来"%name)
                        exit(0)
                    else:
                        if j !=2:
                            print("密码错误,请重新输入,您还有%d次机会"%(2 - j))
                            passwd = input("请输入你的密码:")
                        j += 1
                else:
                    lock_file.write(name + '\n')
                    exit("尝试次数以达3次,请联系管理员")
            else:
                pass
        else:
            if i != 2:
                print("账号%s并不存在,请重新输入"%name)
        i += 1
    else:
        print("账号%s并不存在,退出" % name)
    lock_file.close()
    user_password.close()

     

发表评论

邮箱地址不会被公开。 必填项已用*标注