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

in Python, Having a secure password is a very important practice, when much of o

ID: 3594580 • Letter: I

Question

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:5 pts

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

The program must also ask for a user to enter it and then confirm it:

Enter your password:

Re-enter your password:

If the passwords don’t match or the rules are not fulfilled, the user is prompted again. Your program must include a function that checks whether the rules are passed and the password is valid (returns True if it is valid):

      defisValidPassword(password)

Note: Re-enter password AFTER you validate the password first

Your code with comments

A screenshot of the execution

Test Cases:

            Abcdefg – (should get “must have at least one digit”)

            1BCDEFG – (should get “must have at least one lower case”)

            1abcdefg – (should get “must have at least one upper case”)

            1Abcdef – (should get “must be at least 8 characters long”)

Explanation / Answer

import re
def getpassword():
print 'Enter a New Password: '
password = raw_input("Re - enter your password to Confirm:")
return password
  
def isValidPassword(password):
if len(password) < 8:
print (' The password must be at least 8 characters long')
elif re.search ('[A-Z]',password) is None:
print (' The password must have at least one Uppercase')
elif re.search ('[a-z]',password) is None:
print (' The password must have at least one Lowercase')
elif re.search('[0-9]',password) is None:
print (' The password must have at least one digit')
else:
return True
  
def main():
password = getpassword()
if isValidPassword(password):
print(password, ' is valid')
else:
print(password, ' is invalid')
main()

OUTPUT

Enter a New Password: 1aVfghjk
Re - enter your password to Confirm:1aVfghjk

('1aVfghjk', ' is valid')

Enter a New Password: aVfghjk

Re - enter your password to Confirm: aVfghjk

The password must be at least 8 characters long

('aVfghjk', ' is invalid')