python - 这个 if 循环给了我气

我只是在这段代码底部的 if 循环中遗漏了一些东西。我已经尝试过 ifs 和 buts 和上帝知道的每一种组合,但事情就是不好玩。有人可以帮忙吗?

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")

输出:

>>> %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.

笔记:

文件 ITWorks_password_log.txt 中有三十行。我需要:

  1. 打印每一行,
  2. 使用 if 语句打印一条语句,分别显示短密码和长密码的数量。

我使用以下方法生成了成功的代码:

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

但是我的讲师只想要简单的旧 if loops 来遍历列表并找到 bad_words。

我显然在这里遗漏了一些显而易见的东西。但是我已经尝试了所有我能想到的代码迭代,以使这个血腥的 if 循环工作。有人可以帮忙吗?

回答1

每次迭代时,您都将 bad_word 设置为 [line]。所以它会是这样的:

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]
...

您不是将每一行都添加到数组中,而是在创建一个带有单行的数组,以节省您已经完成的工作。

您需要附加到一个名为 bad_words 的空数组,以便最后一个 for 循环可以检查太短或太长的密码

回答2

在循环之外声明 bad_words ,在 python 中它可以工作,但通常这是不好的做法。

这声明了一个包含单个元素的数组,这不是您想要的:

bad_words = [line]

您想将每一行添加到数组中,请执行以下操作:

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

您还应该关闭打开的文件 https://askubuntu.com/questions/701491/is-closing-a-file-after-having-opened-it-with-open-required-in-python

相似文章

随机推荐

最新文章