Respuesta :
Answer:
Check the explanation
Explanation:
Python program to validate username and passwords and if invalid then fix the password to generate valid passwords
'''
import string
import random
# function to validate username
def username_validation(username):
lowercase_letters = string.ascii_lowercase # string of all lowercase letters
uppercase_letters = string.ascii_uppercase # string of all uppercase letters
digits = string.digits
num_uppercase = 0
num_lowercase = 0
num_digits = 0
num_invalid = 0
for letter in username:
if letter in lowercase_letters:
num_lowercase = num_lowercase + 1
elif letter in uppercase_letters :
�� num_uppercase = num_uppercase + 1
elif letter in digits:
num_digits = num_digits + 1
else:
num_invalid = num_invalid + 1
valid_start_end = not(username[0] in digits) and not(username[-1] in digits)
print('* Length of username: %d'%(len(username)))
print('* All characters are alpha-numeric: '),
print(num_invalid == 0)
print('* First & last characters are not digits: '+str(valid_start_end))
print('* # of uppercase characters in the username: %d' %(num_uppercase))
print('* # of lowercase characters in the username: %d' %(num_lowercase))
print('* # of digits in the username: %d' %(num_digits))
return(valid_start_end,num_uppercase,num_lowercase,num_digits,num_invalid)
# function to validate password
def password_validation(username,password):
while True:
(username_contained,num_lowercase,num_uppercase,num_special_chars,num_digits,num_invalid) = password_validation(username,password)
if(len(password) >= 8 and (not username_contained) and num_lowercase > 0 and num_uppercase > 0 and num_special_chars > 0 and num_invalid == 0):
return password
if num_lowercase == 0:
pos = random.randint(0,len(password)-1)
letter = random.randint(0,len(lowercase_letters)-1)
temp_password = password[0:pos] + lowercase_letters[letter]+ password[pos:]
password = temp_password
print('')
print('* Adding a random lowercase character at a random position: '+password)
if num_uppercase == 0:
pos = random.randint(0,len(password)-1)
letter = random.randint(0,len(uppercase_letters)-1)
temp_password = password[0:pos] + uppercase_letters[letter]+ password[pos:]
password = temp_password
print('* Adding a random uppercase character at a random position: '+password)
if num_special_chars == 0:
pos = random.randint(0,len(password)-1)
letter = random.randint(0,len(special_chars)-1)
temp_password = password[0:pos] + special_chars[letter]+ password[pos:]
password = temp_password
print('* Adding a random special character at a random position: '+password)
if num_digits == 0:
pos = random.randint(0,len(password)-1)
letter = random.randint(0,len(digits)-1)
temp_password = password[0:pos] + digits[letter]+ password[pos:]
password = temp_password
print('* Adding a random digit character at a random position: '+password)
if num_invalid > 0:
i=0
while i <len(password):
if password[i] not in valid_letters:
temp_password = password[0:i] + password[i+1:]
password = temp_password
else:
i = i +1
print('* Removing the invalid character from
(username_contained,num_lowercase,num_uppercase,num_special_chars,num_digits,num_invalid) = password_validation(username,password)
if(len(password) >= 8 and (not username_contained) and num_lowercase > 0 and num_uppercase > 0 and num_special_chars > 0 and num_invalid == 0 and num_digits > 0):
break
else:
print('* Length of password: %d' %(len(password)))
print('* Username is part of password : '+str(username_contained))
print('* # of uppercase characters in the password: %d' %(num_uppercase))
print('* # of lowercase characters in the password: %d' %(num_lowercase))
print('* # of digits in the password: %d' %(num_digits))
print('* # of special characters in the password: %d'%(num_special_chars))
print('* # of invalid characters in the password: %d' %(num_invalid))
print('Password is invalid, please try again')
choice = raw_input('\nWould you like us to fix your password for you? ')
if choice.lower() == 'yes':
password = password_fixer(username,password)
print('Your new password is '+password)
else:
password = raw_input('\nEnter a password: ')
print('Password is valid!')
#call the main function
if __name__ == "__main__":
main()
#end of program
###The full code is more than 5000 characters which is above brainly character limit, so kindly check the below image for the complete code screenshot.



