tags

Title must be at least 15 characters


Body must be at least 30 characters; you entered 16.


Solution

  • Well there is many ways to change this code and make it more beginner friendly.

    You can split the first line in multiple, easier, lines.

    smallerSix = 0
    greaterTen = 0
    
    with open("passwordlog.txt") as log_file:
        lines = log_file.read()      
        lines = lines.splitlines()
        for item in lines:
            if "< 6" in item:
                smallerSix += 1
            if "> 10" in item:
                greaterTen += 1
    
    print("Amount of invalid passwords below minimum length:", smallerSix)
    print("Amount of invalid passwords above maximum length:", greaterTen)
    

    with open(".txt") as x opens a file and creates a reference x. With that reference you can edit the file content or read the file content out.

    lines = log_file.read() reads out the content of the log_file and copyies it's values into lines

    lines = lines.splitlines() splits the String lines at every row which creates a list. Each "place" of the list contains a line (String)

    Now you have a list that has every line of the log_file. Next step is to search each line for a "< 6" and a "> 10". We can do that by going through each element of the array and check the string for this specific string. To do that we can loop through each element of the list: for item in lines. Now we check each item for the specific string "< 6" and "> 10". If one of them is in item, then the variable that contributes to it is being increased by 1.

    At the end you just print out the variable values.