python入门作业——第二周周作业:登录程序

编写用户登录接口
  1、输入账号密码完成验证,验证通过后输出"登录成功"
  2、可以登录不同的用户
  3、同一账号输错三次锁定,(提示:锁定的用户存入文件中,这样才能保证程序关闭后,该用户仍然被锁定)

# user_list =[
#     {'name':'egon','pwd':'123'},
#     {'name':'lili','pwd':'123'},
#     {'name':'sa','pwd':'123'},
#     {'name':'tom','pwd':'123'},
#     {'name':'tank','pwd':'123'}
# ]
count = 0
count_1 = 3
user_name = input('输入账号:').strip()
with open('db.txt',mode='rt',encoding='utf-8') as f:

    # for i in user_list: # 定义列表中的账号密码
        # info_name = i['name']
        # info_pwd = i['pwd']
    for i in f: # 定义文本中的账号密码
        info_name,info_pwd =i.strip().split(':')
        if user_name == info_name:
            with open('black.txt',mode='r+t',encoding='utf-8') as f1:
                for n in f1:
                    # print( user_name,n,end='') # 验证输入账号跟黑名单账号
                    name1 = n.strip() # 因为文件中每一行默认有个换行'
' 所以要加strip去空格
                    if user_name == name1:
                        print('该账号被锁定')
                        break
                else:
                    print('
该账号没有被锁定,可以输入密码')
                    while count < 3:
                        print('你还有%s次机会' %(count_1))
                        user_pwd = input('输入密码:')
                        if user_pwd == info_pwd :
                            print('登录成功')
                            break
                        else:
                            print('密码错误')
                            count +=1
                            count_1 -=1
                    else:
                        print('密码错误过多,该账号已被锁定')
                    with open('black.txt',mode='at+',encoding='utf-8') as f2:
                        f2.write('
%s'%(user_name))
            break
    else:
        print('没有该账号')

  

   

编写程序实现用户注册后,可以登录

while True:
    msg = """
    0 退出
    1 登录
    2 注册
    """
    print(msg)
    cmd = input('请输入命令编号>>: ').strip()
    if not cmd.isdigit():
        print('必须输入命令编号的数字,傻叉')
        continue

    if cmd == '0':
        break

    elif cmd == '1':
        count = 0
        count_1 = 3
        user_name = input('输入账号:').strip()
        with open('db.txt', mode='rt', encoding='utf-8') as f:

            # for i in user_list: # 定义列表中的账号密码
            # info_name = i['name']
            # info_pwd = i['pwd']
            for i in f:  # 定义文本中的账号密码
                info_name, info_pwd = i.strip().split(':')
                if user_name == info_name:
                    with open('black.txt', mode='r+t', encoding='utf-8') as f1:
                        for n in f1:
                            # print( user_name,n,end='') # 验证输入账号跟黑名单账号
                            name1 = n.strip()  # 因为文件中每一行默认有个换行'
' 所以要加strip去空格
                            if user_name == name1:
                                print('该账号被锁定')
                                break
                        else:
                            print('
该账号没有被锁定,可以输入密码')
                            while count < 3:
                                print('你还有%s次机会' % (count_1))
                                user_pwd = input('输入密码:')
                                if user_pwd == info_pwd:
                                    print('登录成功')
                                    break
                                else:
                                    print('密码错误')
                                    count += 1
                                    count_1 -= 1
                            else:
                                print('密码错误过多,该账号已被锁定')
                            with open('black.txt', mode='at+', encoding='utf-8') as f2:
                                f2.write('
%s' % (user_name))
                    break
            else:
                print('没有该账号')
        pass
    elif cmd == '2':
        name = input('注册账号:')
        with open('db.txt',mode='r+t',encoding='utf-8') as f:
            for i in f:
                info_name,info_pwd= i.strip().split(':')
                if name == info_name:
                    print('账号已注册')
                    break
            else:
                password = input('注册密码:')
                with open('db.txt',mode='at',encoding='utf-8') as n:
                    n.write('{}:{}
'.format(name,password))
                    print('完成注册')
        pass
    else:
        print('输入的命令不存在')
原文地址:https://www.cnblogs.com/liuxinging/p/12497674.html