In this exercise, you are asking the user to set a alpha numeric password for any website. Put some conditions.
Password must be 8 characters long.
There must be at-least one uppercase letter.
There must be at least one number.
Then show the user, you have set the correct password or not.
Sample output (Ex 6 and 7): Top 2 are for Ex-6 and bottom 2 are for Ex-7
Create your password which must have these.
Must be 8 characters long.
At least one uppercase letter.
At least one number.
Enter your password: Nidhi123
You have set the correct password.
Create your password which must have these.
Must be 8 characters long.
At least one uppercase letter.
At least one number.
Enter your password: Nidhi12!
You have set the correct password.
Create your password which must have these.
Must be 8 characters long.
At least one uppercase letter.
At least one number.
Create your password which must have these.
Must be 8 characters long.
At least one uppercase letter.
At least one number.
Enter your password: nidhi123
Your password doesn't meet the criteria.
Uppercase is missing.
Create your password which must have these.
Must be 8 characters long.
At least one uppercase letter.
At least one number.
Enter your password: nidhi 234
Your password doesn't meet the criteria.
Uppercase is missing.
It's not 8 characters long correct size.

Answer :

MrRoyal

Answer:

The program in Python is as follows:

print("Create your password which must have these.\nMust be 8 characters long.\nAt least one uppercase letter.\nAt least one number.")

pwd = input("Enter your password: ")

chkLength = 0; chkUpper = 0; chkLower = 0; chkDigit = 0

if len(pwd) == 8:

   chkLength = 1

if (any(x.isupper() for x in pwd)):

   chkUpper = 1

if any(y.islower() for y in pwd):

   chkLower = 1

if any(z.isdigit() for z in pwd):

   chkDigit = 1

if chkLength == 1 and chkUpper == 1 and chkLower == 1 and chkDigit == 1:

   print("You have set the correct password.")

else:

   print("Your password doesn't meet the criteria.")

   if chkLength == 0:

       print("It's not 8 characters long correct size.")

   if chkDigit == 0:

       print("Digit is missing.")

   if chkLower == 0:

       print("Lowercase is missing.")

   if chkUpper == 0:

       print("Uppercase is missing.")

Explanation:

This prints the instruction

print("Create your password which must have these.\nMust be 8 characters long.\nAt least one uppercase letter.\nAt least one number.")

This prompts the user for password

pwd = input("Enter your password: ")

This initializes check variables to 0 (0 - false)

chkLength = 0; chkUpper = 0; chkLower = 0; chkDigit = 0

If password length is 8

if len(pwd) == 8:

Set check variable of length to 1 (1 - true)

   chkLength = 1

If password has uppercase

if (any(x.isupper() for x in pwd)):

Set check variable of uppercase to 1

   chkUpper = 1

If password has lowercase

if any(y.islower() for y in pwd):

Set check variable of lowercase to 1

   chkLower = 1

If password has digit

if any(z.isdigit() for z in pwd):

Set check variable of digits to 1

   chkDigit = 1

If all check variables is 1, then the password is correct

if chkLength == 1 and chkUpper == 1 and chkLower == 1 and chkDigit == 1:

   print("You have set the correct password.")

If otherwise,

else:

The password is incorrect

   print("Your password doesn't meet the criteria.")

The following prints the corresponding criteria that is not met, depending on the value of its check variable

   if chkLength == 0:

       print("It's not 8 characters long correct size.")

   if chkDigit == 0:

       print("Digit is missing.")

   if chkLower == 0:

       print("Lowercase is missing.")

   if chkUpper == 0:

       print("Uppercase is missing.")