如何在python if循环中生成增量计数器

如何在python if循环中生成增量计数器,python,authentication,counter,Python,Authentication,Counter,所以,我正在写一段用户名和密码验证代码,我需要一个计数器来计算有多少次失败的尝试 这是我目前的代码 if username_ask in col0: for k in range(0, len(col0)): if col0[k] == username_ask and col1[k] == "b'"+text+"'": messagebox.showinfo('Welcome, '+

所以,我正在写一段用户名和密码验证代码,我需要一个计数器来计算有多少次失败的尝试

这是我目前的代码

        if username_ask in col0:
            for k in range(0, len(col0)):
                if col0[k] == username_ask and col1[k] == "b'"+text+"'":
                    messagebox.showinfo('Welcome, '+username_ask,'You succesfully logged in to your account: '+username_ask)
                    logincorrect == True
                    found = True
                    root.destroy()
                    sys.exit()
                    count = 0
                else:
                    found = False
            else:
                messagebox.showerror('Incorrect','The credinetials you entered are incorrect, please try again.')
                break
        else:
            messagebox.showerror('Error 404: Not found.','The credentials you enetered do not exist, please try again.')
            sys.exit()
我需要计数器在循环重新启动时保持其值


欢迎提供任何帮助,谢谢您的时间。

对于您正在尝试的操作,这似乎是为应用程序所做的事情。为了解决您的问题,我相信您需要在MainWindow类中创建一个变量(如果是这种情况,假设您正在处理桌面应用程序)

因此,MainWindow类应该如下所示:

## Main Window Class
class MainWindow(object):
    def __init__(self, *args, **kwargs):
        super(MainWindow, self).__init__(*args, **kwargs)
        # Declare here your login counter variable
        self.loginCounter = 0
从这个意义上讲,您需要在登录逻辑中添加如下self.loginCounter变量:


        if username_ask in col0:
            for k in range(0, len(col0)):
                if col0[k] == username_ask and col1[k] == "b'"+text+"'":
                    messagebox.showinfo('Welcome, '+username_ask,'You succesfully logged in to your account: '+username_ask)
                    logincorrect == True
                    found = True
                    root.destroy()
                    sys.exit()
                    count = 0
                else:
                    found = False
            else:
                messagebox.showerror('Incorrect','The credinetials you entered are incorrect, please try again.')
                ## If this part of the code is **inside** the MainWindow Class use:
                self.loginCounter += 1
                ## If this part of the code is **outside** the MainWindow Class use:
                MainWindow.loginCounter +=1
                break
        else:
            messagebox.showerror('Error 404: Not found.','The credentials you enetered do not exist, please try again.')
            sys.exit()

让我知道这是否有帮助!祝你好运

这是一个web服务终端应用程序吗?@AlbertoBonsanto不,它不是终端应用程序。timr geting的计数号401@sahasrara62是的,我不算401你的问题是什么?Stack Overflow不是免费的代码编写服务,也不是为了提供教程或指南。请看。