Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Programming in Python Having a secure password is a very important practice, whe

ID: 3911685 • Letter: P

Question

Programming in Python

Having a secure password is a very important practice, when much of our information is stored online. Write a program that validates a new password, following these rules:

*The password must be at least 8 characters long.

*The password must have at least one uppercase and one lowercase letter.

*The password must have at least one digit.

Write a program that asks for a password, then asks to confirm it. If passwords don't match or the rules are not fulfilled, prompt again. Your program should include a function that checks whether a password is valid.

*must use len() function to test password length.

*loop over each character (that can be a string) and use if statements to test for upper and lower characters as well as digits.

*the tests should be done as functions returning either true or false for the above rules.

Explanation / Answer

import re
def password():
pwd = raw_input("Enter a Password: ")
return pwd
  
def pwd_validity(pwd):
if len(pwd) < 8:
print 'Your password is not perfect'
print 'Your password should have more than 8 characters in length'
elif re.search('[a-z]',pwd) is None:
print 'Your password is not perfect'
print 'Your password should have atleast one lowercase character'
elif re.search('[A-Z]',pwd) is None:
print 'Your password is not perfect'
print 'Your password should have atleast one uppercase character'
elif re.search('[0-9]',pwd) is None:
print 'Your password is not perfect'
print 'Your password should have atleast on digit'
else:
return True
  
def main():
pwd = password()
if pwd_validity(pwd):
print(pwd, 'is valid')
else:
print(pwd, 'is not valid')
main()

OUTPUT

Enter a Password: k345678J
('k345678J', 'is valid')

OUTPUT

Enter a Password: k345678
Your password is not perfect
Your password should have more than 8 characters in length
('k345678', 'is not valid')