zoukankan      html  css  js  c++  java
  • 写代码: 编写登录接口

    # 编写登录接口
    # 基础需求:
    # 1. 让用户输入用户名密码
    # 2. 认证成功后显示欢迎信息
    # 3. 输错三次后退出程序

    # 升级需求:
    # 1. 可以支持多个用户登录(提示, 通过列表存多个账户信息)
    # 2. 用户3次认证失败后,退出程序,再次启动程序尝试登录时,还是锁定状态(提示:需把用户锁定的状态存到文件里)

    ==方法1==

    # define a counter
    count = 0
    
    # define user and password map with dict
    user_password = {'linda': '123', 'emily': '123', 'yu': '123'}
    
    while True:
        login_user = input('Please enter your account: ')
    
        # Read the local_file.
        lock = open('local_file', 'r+')
        lock_file = lock.read().split()
    
        # Verify whether the user is locked or not.
        if login_user in lock_file:
            print('------------------------------------------------
    '
                  'Sorry, your account {} is locked.
    '.format(login_user))
            continue
        # Verify the user exists or not.
        elif login_user not in user_password.keys():
            print('------------------------------------------------
    '
                  'Sorry, your account {} does not exist.
    '.format(login_user))
            continue
        while True:
            login_pwd = input('Please enter your password: ')
            # Verify the password correct or not.
            if login_user in user_password.keys() and login_pwd in user_password.values():
                print("welcome {} to login!".format(login_user))
                exit()
            else:
                count += 1
                times = 3 - count
                if count == 3:
                    print(
                        '------------------------------------------------
    '
                        'Sorry, you already entered 3 times, the account is locked.
    ')
                    # Write the account into lock_file
                    lock.write('{}
    '.format(login_user))
                    # Close file
                    lock.close()
                    flag = True  # drop the second cycle
                    break
                print(
                    '------------------------------------------------
     '
                    'Sorry, your password is not correct. You have {} times
    '.format(times))
        if flag:
            break
    

      ==方法2==

    try_times = 0
    user_password = {'linda': '123', 'emily': '123', 'yu': '123'}
    
    lock = open('lock_user.txt', 'r+')
    lock_file = lock.read().split()
    
    username = input("could you enter your name?: ")
    
    if username in lock_file:
        print("your account is locked, could not login right now. ")
        exit()
    else:
        while try_times < 3:
    
            password = input("please enter your password:")
    
            if username in user_password.keys() and password in user_password.values():
                print("welcome {} to login!".format(username))
                break
            elif username in user_password.keys() and password not in user_password.values():
                print("password is not correct")
    
            elif username not in user_password.keys():
                print("failed login with invalid username")
    
            try_times += 1
    
    while try_times == 3:
        print("sorry, try 3 times, locked your account.")
        f = open("lock_user.txt", 'a')
        f.write('{}
    '.format(username))
        f.close()
        break
    

      

  • 相关阅读:
    MCS锁——可伸缩的自旋锁
    The Art of Multiprocessor Programming读书笔记 (更新至第3章)
    在Visual Studio 2015的Cordova项目中使用Gulp
    SharePoint服务器端对象模型 之 使用LINQ进行数据访问操作(Part 1)
    SharePoint服务器端对象模型 之 使用CAML进行数据查询(Part 4)
    knockoutJs在移动设备上有时无法更新控件值
    SharePoint服务器端对象模型 之 使用CAML进行数据查询(Part 3)
    SharePoint服务器端对象模型 之 使用CAML进行数据查询(Part 2)
    SharePoint服务器端对象模型 之 使用CAML进行数据查询
    SharePoint服务器端对象模型 之 访问文件和文件夹(Part 4)
  • 原文地址:https://www.cnblogs.com/demilyc/p/9985228.html
Copyright © 2011-2022 走看看