Write a PYTHON program with a main and a sub function called checkPassword that
ID: 3760742 • Letter: W
Question
Write a PYTHON program with a main and a sub function called checkPassword that checks whether a string is a valid password. The main function should get the user input as a string.
Define the function as def checkPassword(password). This function returns whether the password is valid or invalid. Use string methods.
Password rules: The password must
• have at least eight characters.
• contain at least two digits.
• must contain at least one uppercase letter
• must contain at least one lowercase letter
• must contain at least one of [!,@,#,$,%,^,&,*,(,)] (non-letter or non-number character)
o Create a list for these special characters
Expected output: Print one of the following based on the value returned from the function. The password you entered is valid The password you entered is invalid
Explanation / Answer
def checkPassword(password):
digitCount = 0
uppercase = 0
lowercase = 0
specialCharacter = 0
nonletter = ['!','@','#','$','%','^','&','*','(',')']
if(len(password)>=8):
for i in range(0,len(password)):
temp = ord(password[i])
if temp>=65 and temp<=90:
uppercase = uppercase + 1
if temp>=97 and temp<=122:
lowercase = lowercase+1
if temp>=48 and temp<=57:
digitCount = digitCount+1
if password[i] in nonletter:
specialCharacter = specialCharacter+1
if digitCount>=2 and uppercase>=1 and lowercase>=1 and specialCharacter>=1:
print 'Valid'
else:
print 'Not Valid'
password = raw_input('Enter a password : ')
checkPassword(password)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.