Home > Back-end >  My second IF statement in this password checker isn't working and I'm unsure why
My second IF statement in this password checker isn't working and I'm unsure why

Time:04-01

Ok, below is my code and I'm just puzzled as to why it can't validate if the password is all letter or numbers. The else statement works fine. Thank you for any help, I really appreciate it!

import datetime

MIN_PASSWORD_LENGTH = 6
MAX_PASSWORD_LENGTH = 10
password = input("Please create a password: ")
password_length = len(password)
PASSWORD_LOG_FILE = open('password_log.txt', "w")

while password_length < MIN_PASSWORD_LENGTH or password_length > MAX_PASSWORD_LENGTH:
    if password_length < MIN_PASSWORD_LENGTH:
        current_date_and_time = datetime.datetime.now()
        error = (f"{current_date_and_time}, password < 6")
        PASSWORD_LOG_FILE.write(error)
        PASSWORD_LOG_FILE.write("\n")
    elif password_length > MAX_PASSWORD_LENGTH:
        current_date_and_time = datetime.datetime.now()
        error = (f"{current_date_and_time}, password > 10")
        PASSWORD_LOG_FILE.write(error)
        PASSWORD_LOG_FILE.write("\n")
    print(f"Your password is {password_length} characters long, but it should be between 6-10.")
    password = input("Please create a valid password: ")
    password_length = len(password)

PASSWORD_LOG_FILE.close()

 if password.isdigit():
    message = "Password weak - contains only numbers."
 elif password.isalpha():
    message = "Password weak - contains only letters."
 else:
    message = "Password strong"

    print(f"Your password is {password_length} characters long. {message}")

    PASSWORD_LOG_FILE = open('password_log.txt', 'r')
    for line in PASSWORD_LOG_FILE:
        print(line, end='')
    PASSWORD_LOG_FILE.close()

CodePudding user response:

Your indentation is wrong, the print statement falls into else: so it only print when password is strong.

You should move the print statement

if password.isdigit():
    message = "Password weak - contains only numbers."
elif password.isalpha():
    message = "Password weak - contains only letters."
else:
    message = "Password strong"
print(f"Your password is {password_length} characters long. {message}")

CodePudding user response:

The last block of code was in the else:, so the message wasn't printed for the isdigit() or isalpha() cases. Also, I can see that you are having some irregular indentation in your code. Kindly fix that.

import datetime

MIN_PASSWORD_LENGTH = 6
MAX_PASSWORD_LENGTH = 10
password = input("Please create a password: ")
password_length = len(password)
PASSWORD_LOG_FILE = open('password_log.txt', "w")

while password_length < MIN_PASSWORD_LENGTH or password_length > MAX_PASSWORD_LENGTH:
    if password_length < MIN_PASSWORD_LENGTH:
        current_date_and_time = datetime.datetime.now()
        error = (f"{current_date_and_time}, password < 6")
        PASSWORD_LOG_FILE.write(error)
        PASSWORD_LOG_FILE.write("\n")
    elif password_length > MAX_PASSWORD_LENGTH:
        current_date_and_time = datetime.datetime.now()
        error = (f"{current_date_and_time}, password > 10")
        PASSWORD_LOG_FILE.write(error)
        PASSWORD_LOG_FILE.write("\n")
    print(f"Your password is {password_length} characters long, but it should be between 6-10.")
    password = input("Please create a valid password: ")
    password_length = len(password)

PASSWORD_LOG_FILE.close()

if password.isdigit():
 message = "Password weak - contains only numbers."
elif password.isalpha():
 message = "Password weak - contains only letters."
else:
 message = "Password strong"

print(f"Your password is {password_length} characters long. {message}")

PASSWORD_LOG_FILE = open('password_log.txt', 'r')
for line in PASSWORD_LOG_FILE:
   print(line, end='')
PASSWORD_LOG_FILE.close()

CodePudding user response:

like people mentiond in the comments, the print statement you are expecting is inside the else block, change it into this:

if password.isdigit():
 message = "Password weak - contains only numbers."
elif password.isalpha():
 message = "Password weak - contains only letters."
else:
 message = "Password strong"
print(f"Your password is {password_length} characters long. {message}")
  • Related