In: Computer Science
[10, 3, 15, 18]
4.
Password Checker
Write a fuction called
passwordChecker()
that asks the user to input a string and
checks if the string can be used as a password. Loop
until the user inputs an
appropriate password. A password must satisfy the
following conditions:
(a) Must contain at least 12 characters
(b) Must contain one special symbol from
string.punctuation [HINTS: use the
string
module; use the keyword
in
]
(c) Must contain at least one lower-case
letter
(d)
Hi,
Hope you are doing fine. I have code the above question in python. All the conditions in the program have been met and the clear explanation and logic has been explained using the comments which have been highlighted in bold.
Program:
#importing string library for
string.punctuation
import string
#function passwordChecker
def passwordChecker():
#The loop is continuosly executed until we use a break
statement
while True:
#length_flag is used to indicate if the length is greater
than 12 characters
length_flag=0
#special_flag is used to see if the string contains atleast
one special character
special_flag=0
#count is used to count the number of lower case
characters
count=0
#taking input from user
pwd=input('Enter the password: ')
#if length of string > 12
if len(pwd)>=12:
#flag is set to 1
length_flag=1
#iterating each character of string
for x in range(0,len(pwd)):
#if he character is present in
string.punctuation
if pwd[x] in string.punctuation:
#flag is set to 1
special_flag=1
#if the character is a lower case, then we increase the
count
if pwd[x].islower():
count=count+1
#if both flags are set to 1 and count>0 then we print
that it is a valid password and break out of the
loop
if length_flag==1 and special_flag==1 and count>0:
print("Valid password!")
break
#else
else:
print("Invalid password")
#if the length is less than 12
if length_flag!=1:
print("The password must contain atleast 12 characters")
#if there are no special characters
if special_flag!=1:
print("The password must contain atleast one special
character")
#if there are no lower case characters
if count==0:
print("The password must contain at least one lower-case
letter")
print("Please re-enter\n")
#the loop iterates again asking for input
#calling function from main to check
execution
passwordChecker()
Executable code snippet from jupyter notebook:
Output: