zoukankan      html  css  js  c++  java
  • 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('输入的命令不存在')
  • 相关阅读:
    ABAP Help Document(2):1.2 表达式
    ABAP Help Document(1):1.1关键字
    api——》将.doc文件转成.docx文件后缀,且仅需要输入单个文件绝对路径
    python 更改默认输出 解决编码常出错问题
    爬取法律法规代码(可直接使用)
    python datetime 模块详解
    python 获得日期列表中最大日期(能够剔出不是日期类型)
    博客园页面css
    日期大小比较令解决{strftime('%Y年%m月%d日')}出错问题
    CodeForces
  • 原文地址:https://www.cnblogs.com/liuxinging/p/12497674.html
Copyright © 2011-2022 走看看