0

I have to write a program that reads from a file, appends the data to a list, reads the list and outputs the number of times the following errors appear "password < 6" and "password > 10"

I am having trouble working out the code to get the program to read the strings for those specific words and output how many times they appear.

def main():

    PASSWORD_LOG_FILE = "ITWorks_password_log.txt"

    list_data = []

    input_file = open(PASSWORD_LOG_FILE, "r")

    for line in input_file:
        list_data.append(line)

    input_file.close()

    for error in list_data:
        print(error, end="")

    for error in range(0, len(list_data)):
        count_pw_too_small = list_data.count("password < 6")
        count_pw_too_large = list_data.count("password > 10")

    print("\nThe number of passwords that were under the minimum length: ", count_pw_too_small)
    print("The number of passwords that were over the maximum length: ", count_pw_too_large)

main()

And below is the data from the list its supposed to read:

2019-07-22 16:24:42.843103, password < 6

DevLounge
  • 8,313
  • 3
  • 31
  • 44
Mitch Wood
  • 11
  • 1
  • @ close voters: I don't think that's a valid duplicate. OP is already trying to use the `.count` method. It doesn't work because OP doesn't want list elements that are an exact match, but instead elements that contain a substring. – Karl Knechtel Mar 23 '21 at 07:43
  • @ OP: what code would you write if you only had to check a single item, instead of a list? – Karl Knechtel Mar 23 '21 at 07:44
  • 2
    I've rolled it back as it makes no sense for the people who come to this question. If you found a solution, post it as answer to your own question instead. – DevLounge Mar 24 '21 at 00:41
  • Right, thanks and apologies, have not used this site before! – Mitch Wood Mar 24 '21 at 00:47

1 Answers1

0

After speaking with my lecturer we have come to following solution, which doesn't use the count method but still worked perfectly.

def main():

    PASSWORD_LOG_FILE = "ITWorks_password_log.txt"

    list_data = []

    input_file = open(PASSWORD_LOG_FILE, "r")

    for line in input_file:
        list_data.append(str(line))

    input_file.close()

    count_pw_too_small = 0
    count_pw_too_large = 0

    for error in list_data:
        print(error, end="")
        if "< 6" in error:
            count_pw_too_small += 1
        elif "> 10" in error:
            count_pw_too_large += 1


    print("\nThe number of passwords that were under the minimum length: ", count_pw_too_small)
    print("The number of passwords that were over the maximum length: ", count_pw_too_large)

main()
Mitch Wood
  • 11
  • 1