python课程第一天作业1-模拟登录

第一周作业:

作业1:编写登陆接口

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

流程图:

代码:后来修改过一次:

#!/usr/bin/env python
# -*-conding:utf-8-**
# __Author__:'liudong'
#!/usr/bin/env python
# -*-coding:utf-8-*-
# __author__="Life"
print('You have three times to login,otherwise your account will be locked!')
for i in range(3):
    username = input('Please input your username:')
    lock_file = open('account_lock.txt', 'r')
    lock_list = lock_file.readlines()  # 已经被锁定用户清单文件
    for lock_line in lock_list:     #判断用户输入的名字是否已经锁定(在锁定的文件列表中)
        lock_line = lock_line.strip('
')
        if username == lock_line:
            print('Your account is locked!')
            lock_file.close()
            exit()
    user_account=open('user_account.txt','r')
    user_account_list=user_account.readlines()
    #print(user_account_list)
    for user in user_account_list:
        (user_infile,password_infile)=user.strip('
').split()
        if username == user_infile:
            #print(user_infile)
            j = 0
            while j < 3:
                password = input('Please input your password:')
                if password == password_infile:
                    print('login successed!')
                    user_account.close()
                    lock_file.close()
                    exit()
                else:
                    print('Invalid username or password...')
                    print('this is the %d time(s)' % (j + 1))
                j+=1
            else:
                lock_file = open('account_lock.txt', 'w')
                lock_file.write(username + '
') #锁定用记名写入锁定文件
                print('Your account is locked! Please,contact adminstrator to unlock your account!')

                exit()
    else:
        print('user %s is not exists,please input again:')

lock_file.close()
user_account.close()

 


原文地址:https://www.cnblogs.com/ld1977/p/5967597.html