if function not communicating with while loop

The purpose of this program is for the user to enter a password which is more than 6 but less than 10 characters and check whether it's strong or weak. Whenever the user inputs an invalid password which has less than 6 or more than 10 characters, it will log it in a text file with a timestamp and displaying a message of the length of the password ("password < 6" or "password > 10"). I did this in my code but for some reason, the it keeps on logging a message saying password should be less than 6 and more than 10 characters. This occurs non-stop in the terminal. Here's the photo and code:

code issue

from datetime import datetime  # Greyed out until the package is used in the code

print("PasswordChecker1 program developed by Devang Sahani")


def main():
    PASSWORD_LOG_FILE = "password_log_devang_sahani.txt"
    password_log = open(PASSWORD_LOG_FILE, "w").close()
    MIN_PASSWORD_LENGTH = 6  # Constant. Variable holding numerical value of minimum password characters
    MAX_PASSWORD_LENGTH = 10  # Constant. Variable holding numerical value of maximum password characters
    message_ls = []  # Content list which will be holding all 'messages'
    message = ''  # This will vary as python choose the appropriate 'message' according to the user input
    password_log = open(PASSWORD_LOG_FILE, "a")

# The while loop prior to the if-structure is a statement to control the flow of loops. Whilst it stays true, it
# will loop constantly until the statement results false causing it to break the program. In this case, the program will
# loop until user inputs a password with requirements of minimum and maximum characters.

# The if-structure below allows python to use a range of tools for different tasks that is asked within the code. This
# includes tools like the 'isalpha()' or 'isnumeric()' method, which are used below in the structure as it allows python
# check whether the inputted password is all letter or numbers and present the following:

    while True:  # While Loop
        password = input("Please enter your password: ")  # Prompts user to enter password
        password_length = len(password)  # Variable which holds the password character length
        print(password_length)  # Prints the password length of 'password' to inform user
        current_date_and_time = datetime.today()

        while len(password) < MIN_PASSWORD_LENGTH or len(password) > MAX_PASSWORD_LENGTH: # Checks requirement constants

            if password_length < MIN_PASSWORD_LENGTH:
                # If requirements are not met, it says this
                reason_password_invalid = "Password < 6. Should have more than 6 but less 10 characters"
                message = reason_password_invalid
                password_log.write(f"{current_date_and_time}, {message}\n")
            elif password_length > MAX_PASSWORD_LENGTH:
                reason_password_invalid = "Password > 6. Should have more than 6 but less 10 characters"
                message = reason_password_invalid
                password_log.write(f"{current_date_and_time}, {message}\n")
                # If input password meets requirement, it will move on and check if it's all alphabetical
            elif password.isalpha():
                # If yes, then it will display this message and loop
                reason_password_invalid = "password weak – contains only letters"
                message = reason_password_invalid
                password_log.write(f"{current_date_and_time}, {message}\n")
                # If password is not all alphabetical letters, it will use this function to check
            elif password.isnumeric():
                # And if true, it will display this message and loop
                reason_password_invalid = "password weak – contains only numbers"
                message = reason_password_invalid
                password_log.write(f"{current_date_and_time}, {message}\n")
                # If the input is not all alphabetical or numerical, it will move to this else statement
            else:
                # And choose this message
                message = "password strong"
            # Out of all 'message' in the list, it will choose the appropriate one
            message_ls.append(message)
            # And print it here
            print(message)

            # If the print result is this, it will break the code that will end the program
            if message == "password strong":
                print(f"\n Validation Successful. Your password is {password_length} characters.")

                print(f"\n{message}\n")
                password_log = open(PASSWORD_LOG_FILE, "r")

                for x in password_log:
                    print(x, end='')

                break
                password_log.close()


main()


Read more here: https://stackoverflow.com/questions/68466342/if-function-not-communicating-with-while-loop

Content Attribution

This content was originally published by DevAS at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: