Nov-14-2020, 05:43 AM
Hi all I'm new to coding and am currently doing a assignment called Password Checker.
It has been split into 4 parts and I have passed the first 2 and am stuck halfwayish on the third part.
Basically I have to create A program that is required to input and validate a password to ensure it contains the minimum number of characters (6) and a maximum of 10 characters.
For every unsuccessful attempt, the program will record an entry in a password log file that will include the date/time (updated to the nearest microsecond) and the reason why the password in invalid either “password < 6” or “password >10”. The file will not store any passwords.
Once validated, the program will output the length of the password and determine if the password is weak or if the password is strong.
The program will then output all the contents of the password log file to the screen.
My code so far:
def main():
import datetime
print("PasswordChecker2 Program developed by Matthew Costello")
#constants that need to be used
MIN_LENGTH = 6
MAX_LENGTH = 10
PASSWORD_LOG_FILE = "password_log_matthew_costello.txt"
#password input
password = input("Please enter your password for checking: ")
# This part of the code uses len(password) to calculate a passwords length in characters and a while loop to validate
# whether the password meets the min and max parameters and keeps asking for input until satisfied.
while len(password) < MIN_LENGTH or len(password) > MAX_LENGTH:
print("Invalid")
password = input("Please enter your password for checking: ")
# This will opens a .txt file and write the current date and time and a message to why password was invalid.
# Will not store password.
error_log = str(datetime.datetime.now())
output_file = open(PASSWORD_LOG_FILE, "a+")
output_file.write(error_log)
output_file.write("\n")
output_file.close()
# This second part is to determine if a password is all numbers, all letters or a combination of both.
# Outcome of weak or strong.
if password.isnumeric() is True:
comment = "password is weak - contains only numbers."
elif password.isalpha() is True:
comment = "password is weak - contains only letters."
else:
comment = "password is strong."
print("Your password is {:} characters long".format(len(password)))
print("Your {:s}".format(comment))
input_file = open(PASSWORD_LOG_FILE, "r")
for line in input_file:
print(line, end='')
input_file.close()
main()
As you can see I have been able to open text file write to it the datetime and the have contents displayed on screen. But I am unsure how to have the error message displayed as well on same line as date time.
I have tried IF statements, WHILE statements and changing flow of code. I can never seem to get it to work.
Lastly the only other thing I cant get to work is that if an invalid code is entered more than once in a row it only records the most recent attempt to the file now all failed attempts.
Cheers for any help anyone can give.
It has been split into 4 parts and I have passed the first 2 and am stuck halfwayish on the third part.
Basically I have to create A program that is required to input and validate a password to ensure it contains the minimum number of characters (6) and a maximum of 10 characters.
For every unsuccessful attempt, the program will record an entry in a password log file that will include the date/time (updated to the nearest microsecond) and the reason why the password in invalid either “password < 6” or “password >10”. The file will not store any passwords.
Once validated, the program will output the length of the password and determine if the password is weak or if the password is strong.
The program will then output all the contents of the password log file to the screen.
My code so far:
def main():
import datetime
print("PasswordChecker2 Program developed by Matthew Costello")
#constants that need to be used
MIN_LENGTH = 6
MAX_LENGTH = 10
PASSWORD_LOG_FILE = "password_log_matthew_costello.txt"
#password input
password = input("Please enter your password for checking: ")
# This part of the code uses len(password) to calculate a passwords length in characters and a while loop to validate
# whether the password meets the min and max parameters and keeps asking for input until satisfied.
while len(password) < MIN_LENGTH or len(password) > MAX_LENGTH:
print("Invalid")
password = input("Please enter your password for checking: ")
# This will opens a .txt file and write the current date and time and a message to why password was invalid.
# Will not store password.
error_log = str(datetime.datetime.now())
output_file = open(PASSWORD_LOG_FILE, "a+")
output_file.write(error_log)
output_file.write("\n")
output_file.close()
# This second part is to determine if a password is all numbers, all letters or a combination of both.
# Outcome of weak or strong.
if password.isnumeric() is True:
comment = "password is weak - contains only numbers."
elif password.isalpha() is True:
comment = "password is weak - contains only letters."
else:
comment = "password is strong."
print("Your password is {:} characters long".format(len(password)))
print("Your {:s}".format(comment))
input_file = open(PASSWORD_LOG_FILE, "r")
for line in input_file:
print(line, end='')
input_file.close()
main()
As you can see I have been able to open text file write to it the datetime and the have contents displayed on screen. But I am unsure how to have the error message displayed as well on same line as date time.
I have tried IF statements, WHILE statements and changing flow of code. I can never seem to get it to work.
Lastly the only other thing I cant get to work is that if an invalid code is entered more than once in a row it only records the most recent attempt to the file now all failed attempts.
Cheers for any help anyone can give.