'Counter var doesn't count up

I was working on a function that should count the total amount of e-mails that are in the inbox. everything works but it just doesn't want to count it all up.

I have an database where i check the users emails and username.

Function:

@pyqtSlot(str, str)
def checkaantalmail(self, email, wachtwoord):

    cursor.execute("SELECT DISTINCT Naam FROM Klant")
    updatedklant = str(cursor.fetchall())

    credentials = Credentials(email, wachtwoord)
    acc = Account(email, credentials=credentials, autodiscover=True)

    for item in acc.inbox.all().order_by('-datetime_received')[:500]:

        inboxmail = str(item.sender).split("'")
        currentinboxmail = inboxmail[3]

        cursor.execute("SELECT DISTINCT Klant FROM Mail WHERE Mail=?", currentinboxmail)
        currentklant = str(cursor.fetchall())

        remove_characters = ["(",")","'",",","]","["]
        for characters in remove_characters:
            currentklant = currentklant.replace(characters, "")

        counter = 0 

        if currentklant not in updatedklant:
            print("yes")
            print(currentklant)
            counter += 1

    print("total", counter)

cmd prints:

if: works good and prints the current customer full name

total counter: prints 1 or 0 depends what the last e-mail is

Thanks in advance.



Solution 1:[1]

You are resetting counter within the loop. counter = 0 should be outside the loop.

– Maurice Meyer

@pyqtSlot(str, str)
def checkaantalmail(self, email, wachtwoord):

    cursor.execute("SELECT DISTINCT Naam FROM Klant")
    updatedklant = str(cursor.fetchall())

    credentials = Credentials(email, wachtwoord)
    acc = Account(email, credentials=credentials, autodiscover=True)

    counter = 0 

    for item in acc.inbox.all().order_by('-datetime_received')[:500]:

        inboxmail = str(item.sender).split("'")
        currentinboxmail = inboxmail[3]

        cursor.execute("SELECT DISTINCT Klant FROM Mail WHERE Mail=?", currentinboxmail)
        currentklant = str(cursor.fetchall())

        remove_characters = ["(",")","'",",","]","["]
        for characters in remove_characters:
            currentklant = currentklant.replace(characters, "")

        if currentklant not in updatedklant:
            print("yes")
            print(currentklant)
            counter += 1

            
    print("total", counter)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1