Python登录计数器

Python登录计数器,python,Python,我是Python新手,正在尝试创建一个带有仿OTP系统和登录限制的简单登录。OTP可以工作,但登录限制计数器不能工作。它给了我期望的结果后,只有一次失败的尝试我想要3。尝试失败后的输出为: 访问被锁定。你不配看到这里有什么。再见 用户名或密码不正确。请再试一次 用户名或密码不正确。请再试一次 用户名或密码不正确。请再试一次 进程已完成,退出代码为1 代码如下: def old_acc(): count = 0 # count created to limit number of failed

我是Python新手,正在尝试创建一个带有仿OTP系统和登录限制的简单登录。OTP可以工作,但登录限制计数器不能工作。它给了我期望的结果后,只有一次失败的尝试我想要3。尝试失败后的输出为:

访问被锁定。你不配看到这里有什么。再见

用户名或密码不正确。请再试一次

用户名或密码不正确。请再试一次

用户名或密码不正确。请再试一次

进程已完成,退出代码为1

代码如下:

def old_acc():
count = 0  # count created to limit number of failed logins
login = input("Username:    ")  # prompts user to login in with their username
pw = input("Password:   ")  # prompts user to login in with their password
while count <= 3:
    for line in open("db.txt", "r").readlines():
        acc_info = line.split()

        # if username and pw do not match, prompt user to try again
        if login != acc_info[0] and pw != acc_info[1]:
            print("\nIncorrect Username or Password. Please try again.\n")
            count += 1
        # if username and pw match, login is successful; generate otp
        else:
            gen_otp()
            print("ACCESS GRANTED")
            access_info()

        # if failure count is = 3, deny access and lock out.
        if count == 3:
            # stops code and doesn't allow any further input.
            sys.exit("ACCESS LOCKED. YOU DON'T DESERVE TO SEE WHAT'S HERE. GOODBYE.")

谢谢。

for循环检查db.txt文件中的每一行,如果与密码不匹配,则增加计数器。鉴于db.txt可能包含多个密码,计数器在第一次尝试时将已达到4。仅当db.txt中没有与密码匹配的行时,才需要增加计数器

def old_acc():
   count = 0  # count created to limit number of failed logins
   success = False # keeps track of succesful login
   while count <= 3 and not success:
      login = input("Username:    ")  # prompts user to login in with their username
      pw = input("Password:   ")  # prompts user to login in with their password

       for line in open("db.txt", "r").readlines():
          acc_info = line.split()

           # if username and pw match, login is successful; generate otp
           if login == acc_info[0] and pw == acc_info[1]:
               gen_otp()
               print("ACCESS GRANTED")
               access_info()
               success = True
               break

        # if username and pw do not match, prompt user to try again
    if not success:
        print("\nIncorrect Username or Password. Please try again.\n")
        count += 1

    # if failure count is = 3, deny access and lock out.
    if count == 3:
        # stops code and doesn't allow any further input
        sys.exit("ACCESS LOCKED. YOU DON'T DESERVE TO SEE WHAT'S HERE. GOODBYE.")
修正了gen_otp的压痕
def old_acc():
   count = 0  # count created to limit number of failed logins
   success = False # keeps track of succesful login
   while count <= 3 and not success:
      login = input("Username:    ")  # prompts user to login in with their username
      pw = input("Password:   ")  # prompts user to login in with their password

       for line in open("db.txt", "r").readlines():
          acc_info = line.split()

           # if username and pw match, login is successful; generate otp
           if login == acc_info[0] and pw == acc_info[1]:
               gen_otp()
               print("ACCESS GRANTED")
               access_info()
               success = True
               break

        # if username and pw do not match, prompt user to try again
    if not success:
        print("\nIncorrect Username or Password. Please try again.\n")
        count += 1

    # if failure count is = 3, deny access and lock out.
    if count == 3:
        # stops code and doesn't allow any further input
        sys.exit("ACCESS LOCKED. YOU DON'T DESERVE TO SEE WHAT'S HERE. GOODBYE.")