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

in python Write a program with a main and a sub function called checkPassword th

ID: 3806591 • Letter: I

Question

in python

Write a 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 and nonnumeric character) o Create a list for these special characters

Explanation / Answer

python 2.7 code:

def checkuppercase(s):
   for i in range(0,len(s)):
       if(ord(s[i]) >= 65 and ord(s[i]) <= 90):
           return True;
   return False;

def checklowercase(s):
   for i in range(0,len(s)):
       if(ord(s[i]) >= 97 and ord(s[i]) <= 122):
           return True;
   return False;

def checkdigit(s):
   occur = 0;
   for i in range(0,len(s)):
       if(ord(s[i]) >= 48 and ord(s[i]) <= 57):
           occur = occur + 1;
   if(occur >= 2):
       return True
   else:
       return False;
def checkspecialchar(s):
   for i in range(0,len(s)):
       if(s[i] in ['!','@','#','$','%','^','&','*','(',')']):
           return True
   return False;

def checkpassword(string):
   if(len(string) < 8):
       print "Invalid password, the password should be at least 8 characters long ";
       return("Invalid")
   if(checkuppercase(string) == False):
       print "Invalid password, the password should contain at least one uppercase letter ";
       return("Invalid")
   if(checklowercase(string) == False):
       print "Invalid password, the password should contain at least one lowercase letter ";
       return("Invalid")
   if(checkdigit(string) == False):
       print "Invalid password, the password should contain at least 2 digits ";
       return("Invalid")
   if((checkspecialchar(string) == False)):
       print "Invalid password, the password should contain atleast one special character";
       return("Invalid")
   print "Valid Password!"
   return("Valid")

print "Enter a string"
string = raw_input().strip()
checkpassword(string)