Expected Output Password Length Report Using the provided passwords.csv file, your report should look like the one below. Your entries may be in a different order.
Process:
1. Ask the user for the name of an input file (a test file is provided).
2. Open the file for input
3. Pass the file to a FUNCTION (buildList) you will define that will:
a. Accept an input file
b. Return a List
c. The function will read the input file, separating individual passwords and storing
the passwords in a List structure.
4. Pass the List created in the previous step to a FUNCTION (buildDictionary) you
will define that will:
a. Accept a List
b. Return a Dictionary
c. Create the Dictionary based on the lengths of the passwords in the List
received. Each entry in the dictionary will consist of a KEY (the password length)
and a VALUE (the count of passwords of that length). See Expected Output for
details.
5. Print a report based on the Dictionary created in step #4.
a. Print report and column headers using provided format lines.
b. Print one line per Dictionary entry, using the provided format line.
c. Expected output for report is included in these instructions.
6. Produce a report indicating the validity or invalidity of each password:
a. Print a report header line using the provided format line.
b. Print individual detail lines for the report by:
Passing each individual string to a FUNCTION (validatePassword) you
will define that will:
1. Accept a string value
2. Return a boolean value
3. Determine whether the received string is a valid password based
on the following rules:
a. The string must be AT LEAST 8 characters long
b. The string must contain AT LEAST 1 UPPER CASE letter
c. The string must contain AT LEAST 1 LOWER CASE letter
d. The string must contain AT LEAST 1 instance of one of the
following SPECIAL CHARACTERS: $, !, &, *.
4. Return True if the string passes all tests. Return False if the string
fails at least one test.
Write an output line based on the return value form the previous step,
using the provided format lines.
Requirements:
1. Name your code passwordCheck.py.
2. Name the FUNCTION referenced in step #3 buildList.
3. Name the FUNCTION referenced in step #4 buildDictionary.
4. Name the FUNCTION built for step #6b validatePassword.
5. Code to do the following will be contained in the main FUNCTION:
a. Prompt the user for a file name.
b. Open the file for input.
c. Call the FUNCTION buildList, passing the opened file to the function and
assigning the returned List structure to a variable name.
d. Call the FUNCTION buildDictionary, passing the List variable from step
5.c. and assigning the returned Dictionary structure to a variable name.
e. Print the report as described in Process step #5,
f. Print the heading for the report in Process step #6.
g. “Loop” through the List structure saved in step 5.c., calling the FUNCTION
validatePassword for each string in the List, and outputting an appropriate
line based on the return from the function call.
6. Code a correct call to the main FUNCTION at the end of your script
Here is what I have so far:
def buildList(fin): pswds = [] for index, line in enumerate(fin): if index == 0: # skipping first row, HEADERS continue pswds.append(line.strip()) return pswds def buildDictionary(pswds): pdict = {} len_pswd = [] for element in pswds: len_pswd.append(len(element)) for i in pswds: pdict[len(i)] = len_pswd.count(len(i)) return pdict def validatePassword(pswd): upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" lower = "abcdefghijklmnopqrstuvwxyz" special = "$!&*" upperFlag = False lowerFlag = False specialFlag = False for i in upper: if i in pswd: upperFlag = True if i.lower() in pswd: lowerFlag = True for i in special: if i in pswd: specialFlag = True return len(pswd) >= 8 and lowerFlag and upperFlag and specialFlag if __name__ == '__main__': filename = input("Enter input file name: ") fin = open(filename, "r") pswds = buildList(fin) pdict = buildDictionary(pswds) print("\n\nPassword Length Dictionary Entries") print("%9s%20s" % ("Length", "Count")) for key in pdict.keys(): print("%9s%20s" % (str(key), str(pdict[key]))) print("\n\n%62s" % "Validate Passwords in Check File\n") for pswd in pswds: valid = validatePassword(pswd) if valid: print("%40s\tValid" % pswd) else: print("%40s\tINVALID" % pswd)
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 5 images