Home > Net >  This if loop is giving me gas
This if loop is giving me gas

Time:05-19

I'm just missing something in the if loop at the bottom of this code. I have tried every single combination of ifs and buts and god-knows, and the thing just ain't playing nice. Could somebody please help?

def main():
    print("Password Checker Program 3\nDeveloped by Benjamin Roberts\n17/May/2022\n")

    short_password = 0
    long_password = 0
    TOO_SHORT = str("< 6")
    TOO_LONG = str("> 10")

    temp_file = open("ITWorks_password_log.txt", "r")
    for line in temp_file:
        bad_words = [line]
        print(line)

    for line in bad_words:
        if str(TOO_SHORT) in line:
            short_password = short_password   1
        if str(TOO_LONG) in line:    
            long_password = long_password   1
    print(f"There are {short_password} passwords that are too long.\n")        
    print(f"There are {long_password} passwords that are too short.\n")

Outputs:

>>> %Run PASSWORD_CHECKER3_BEN_ROBERTS.py
Password Checker Program 3
Developed by Benjamin Roberts
17/May/2022

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

2021-07-22 16:24:44.963020, password < 6

2021-07-22 16:24:49.327202, password > 10

2021-07-22 16:24:52.810838, password > 10

2021-07-22 16:24:57.057562, password > 10

2021-07-22 16:24:58.961836, password < 6

2021-07-22 16:25:00.181773, password < 6

2021-07-22 16:25:01.408118, password < 6

2021-07-22 16:25:02.629833, password < 6

2021-07-22 16:25:03.484845, password < 6

2021-07-22 16:25:04.208258, password < 6

2021-07-22 16:25:04.939255, password < 6

2021-07-22 16:25:05.643320, password < 6

2021-07-22 16:25:09.730280, password > 10

2021-07-22 16:25:33.497937, password < 6

2021-07-22 16:25:34.746323, password < 6

2021-07-22 16:26:00.286218, password > 10

2021-07-22 16:26:02.060182, password < 6

2021-07-22 16:26:07.114983, password > 10

2021-07-22 16:26:11.971780, password > 10

2021-07-22 16:26:12.941778, password < 6

2021-07-22 16:26:13.789452, password < 6

2021-07-22 16:26:14.744570, password < 6

2021-07-22 16:26:15.676765, password < 6

2021-07-22 16:26:33.763707, password < 6

2021-07-22 16:26:35.048542, password < 6

2021-07-22 16:26:36.318447, password < 6

2021-07-22 16:26:38.991601, password < 6

2021-07-22 16:26:43.001912, password > 10

2021-07-22 16:26:46.593327, password > 10



There are 0 passwords that are too long.

There are 0 passwords that are too short.

NOTES:

There are thirty lines in the file ITWorks_password_log.txt. I need to:

  1. print each line,
  2. print a statement using if statements to show the amount of short and long passwords respectively.

I've produced successful code using:

for line in words:
    words = f.read()
    print(bad_words)

but my lecturer only wants simple old if loops to iterate through the list and find the bad_words.

I am obviously missing something plainly obvious here. But I have tried absolutely every single iteration of code I can think of to make this bloody if loop work. Could someone please help?

CodePudding user response:

You are setting bad_word to [line] every time you iterate. So it'll go like this:

line 1: bad_word = [2021-07-22 16:24:42.843103, password < 6]
line 2: bad_word = [2021-07-22 16:24:44.963020, password < 6]
line 3: bad_word = [2021-07-22 16:24:49.327202, password > 10]
...

You aren't adding each line to an array, you are creating an array with one singular line an saving over the work you have already done.

You need to append to an empty array called bad_words so that the last for loop can check for too short or too long passwords

CodePudding user response:

Declare bad_words outside of the loop, in python it works but in general it's bad practice.

This declares an array containing a single element which is not what you want:

bad_words = [line]

You want to add each line to the array so do the following:

bad_words = []
for line in temp_file:
    bad_words.append(line)
    print(line)

You should also close your opened file 1

  • Related