Skip to content
Advertisement

Why is my with statement skipped after while loop?

With this code (I added some prints to test where the code stops) I get this:

do you have your details stored here already? y/n y

Please enter a username Test

debug: username_exists is True

debug: I ran this command

do you have your details stored here already? y/n

import hashlib
import time

while True:
    have_account = input("do you have your details stored here already? y/n    ")
    while have_account not in ("y", "n"):
        print("please type either y or n")
        have_account = input("y/n    ")
    if have_account == "y":
        username_exists = False
        while username_exists is False:
            username_login = input("Please enter a username    ")
            with open("users.txt", "r") as f:
                readfile = f.read()
                if username_login in readfile:
                    username_exists = True
                    print("debug: username_exists is " + str(username_exists))
                else:
                    username_exists = False
                    print("That user does not exist")
        print("debug: I ran this command")
        with open("users.txt", "r") as f:
            readfile = f.read()
            user_loc = readfile.index(username_login)
            user_len = len(username_login)
            f.seek(user_loc + user_len + 3)
            pass_hash = f.read(64)
            password_login_hashed = None
            pass_matches = None
            while pass_matches is False:
                password_login = input("Please enter your password    ")
                password_login_hashed = hashlib.sha256(password_login.encode('utf-8')).hexdigest()
                password_login = None
                if password_login_hashed == pass_hash:
                    pass_matches = True
                    print("Congrats! You logged in!")
                else:
                    pass_matches = False
                    quit_question = input("Incorrect password! Please try again or type "quit" to quit")
                    if quit_question == "quit":
                        quit()

Advertisement

Answer

You’re initializing pass_matches to None and then you’re checking pass_matches is False which will always return False because here pass_matches will always be None

modify the while statement with this line of code:

while pass_matches in (False, None)

Advertisement