mercredi 7 août 2019

One print statement outputs twice

I am trying to write a login module that searches a .csv file for the entered username. If it finds the username it will the take the entered password and pass it through a function to verify it (the password is salted and hashed before being saved into the .csv file).

I ran the below code running through tests for the desired results for username that's in the list and correct password, username that is not in the list, username that is in the list and incorrect password.

with open('userDB.csv', 'rt') as f:
    reader = csv.reader(f, delimiter=',')
    for row in reader:
        for field in row:
            if user == row[0]:
                psChk = row[1]
                if verify_password(psChk, password):
                    print('In')
                else:
                    print('Out!!')
            else:
                print('Out!!')
                sys.exit()

For the test with user in the list and correct password I get the 'In' statement printed twice and when I test for user in the list and incorrect password I get 'Out' twice. While that is the desired result I just want to understand what's going on here.

When I test for username not in the list 'Out' is printed only once.

Aucun commentaire:

Enregistrer un commentaire