Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with my code
#4
If you look up the docs for datetime, you will find it allows you to format the output string in many ways. You can show all or just part of the info.

Open the log file 'a' to append to the existing file, but that will create the file if it is not there.

I think this will do something like what you want. Just some fun on Sunday morning!

from datetime import datetime
# in case you want to do time zone stuff
from zoneinfo import ZoneInfo

path2log = '/home/pedro/temp/password_log.txt'

def logit(password):    
    if len(password) < 6:
        reason = 'too short'
    elif len(password) > 10:
        reason = 'too long'
    elif password.isalpha() or password.isnumeric():
        reason = 'weak'
    else:
        reason = 'strong'
    # only log unacceptable passwords
    if not reason == 'strong':
        dt = datetime.now()
        date = dt.strftime('%A %d-%m-%Y, %H:%M:%S.%f')        
        with open(path2log, 'a') as mylog:            
            mylog.write(f'Password: {password} attempted on {date} declined because it is {reason}. \n')
        return False   
    else:
        print(f'{password} is a good {reason} password! You will be a secret agent someday!')
        return True
        
acceptable = False
while not acceptable:
    print('Please enter a password between 6 and 10 characters long.')
    print('Your password should contain at least 1 non-alphanumeric character.') 
    password = input('Enter your password. It must be a minimum of 6 and a maximum of 10 characters and have at least 1 non-alphanumeric character. \n')
    acceptable = logit(password) 
Reply


Messages In This Thread
Help with my code - by bp12 - Sep-06-2023, 03:09 PM
RE: Help with my code - by deanhystad - Sep-06-2023, 07:24 PM
RE: Help with my code - by bp12 - Sep-07-2023, 10:24 AM
RE: Help with my code - by Pedroski55 - Sep-10-2023, 11:41 AM
RE: Help with my code - by deanhystad - Sep-10-2023, 01:33 PM
RE: Help with my code - by Pedroski55 - Sep-11-2023, 05:15 AM
RE: Help with my code - by deanhystad - Sep-11-2023, 02:49 PM
RE: Help with my code - by bp12 - Sep-17-2023, 02:28 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020