验证并将所有内容写入文件。

huangapple go评论72阅读模式
英文:

Validation and getting everything to write to file

问题

Here's the translated code portion you requested:

import datetime

def main():
    MIN_LENGTH = 6
    MAX_LENGTH = 10
    PASSWORD_LOG_FILE = "password_log.txt"
    password = input("Please enter your password for checking: ")
    password_length = len(password)

    while len(password) < 6 or len(password) > 10:
        print("Invalid")
        error_log = str(datetime.datetime.now())
        output_file = open(PASSWORD_LOG_FILE, "a")
        output_file.write(error_log)
        password = input("Please enter your password for checking: ")
        if password_length < MIN_LENGTH:
            output_file.write("{}, password <6".format(error_log))
        elif password_length > MAX_LENGTH:
            output_file.write("{}, password >10".format(error_log))
        output_file.write("\n")
        output_file.close()

    if password.isnumeric() is True:
        comment = "password is weak - contains only numbers."
    elif password.isalpha() is True:
        comment = "password is weak - contains only letters."
    else:
        comment = "password is strong."

    print("Your password is {:} characters long".format(len(password)))
    print("Your {:s}".format(comment))

    input_file = open(PASSWORD_LOG_FILE, "r")

    for line in input_file:
        print(line, end='')

    input_file.close()

main()

Please note that I have translated the code as requested, and it contains the same logic as the original code. If you have any specific questions about the code or need further assistance, please let me know.

英文:

So I'm working on a password program that logs all incorrect attempts (just the date and time with an error message saying why)
The issue I'm having is when I input a password that is say, >10 and then input a password that is <6...all failed attempts will be logged as >10 even if that isn't the case.

I'm very much a beginner to Python so feel free to explain everything to me like I'm 5.

import datetime


def main():

    MIN_LENGTH = 6
    MAX_LENGTH = 10
    PASSWORD_LOG_FILE = &quot;password_log.txt&quot;
    password = input(&quot;Please enter your password for checking: &quot;)
    password_length = len(password)


    while len(password) &lt; 6 or len(password) &gt; 10:
        print(&quot;Invalid&quot;)
        error_log = str(datetime.datetime.now())
        output_file = open(PASSWORD_LOG_FILE, &quot;a&quot;)
        output_file.write(error_log)
        password = input(&quot;Please enter your password for checking: &quot;)
        if password_length &lt; MIN_LENGTH:
            output_file.write(&quot;{}, password &lt;6&quot;.format(error_log))
        elif password_length &gt; MAX_LENGTH:
            output_file.write(&quot;{}, password &gt;10&quot;.format(error_log))
        output_file.write(&quot;\n&quot;)
        output_file.close()

    if password.isnumeric() is True:
        comment = &quot;password is weak - contains only numbers.&quot;
    elif password.isalpha() is True:
        comment = &quot;password is weak - contains only letters.&quot;
    else:
        comment = &quot;password is strong.&quot;

    print(&quot;Your password is {:} characters long&quot;.format(len(password)))
    print(&quot;Your {:s}&quot;.format(comment))

    input_file = open(PASSWORD_LOG_FILE, &quot;r&quot;)

    for line in input_file:
        print(line, end=&#39;&#39;)

    input_file.close()

main()

I realise an issue may be in the "While" loop and tried moving bits of code around and re-formatting but I either trigger an endless loop or the error message doesn't begin in a new line etc.
As mentioned above, What I would like to happen is that it tells me the password was invalid because it was either <6 or <10. Right now it logs all incorrect attempts as the same as the first attempt - I can't seem to locate the dodgy line of code that is causing this!

答案1

得分: 0

你在开始时存储了 password_length 一次,然后在你的 if 检查中使用它。然而,len(password)password_length 不相同!你在 while 循环中正确使用了 len(password),但在重新输入密码后没有更新 password_length 为新的长度。

你有几种修复这个问题的选项,但我认为最简单的方法可能是在 password = input("请输入要检查的密码:") 后面添加一行 password_length = len(password)

英文:

You store password_length once at the start, and use it in your if check. However, len(password) is not the same as password_length! You correctly use len(password) in your while loop, but you don't update password_length to the new length after you retype a password.

You have a few options of how to fix this, but I think the easiest one may be to add the line password_length = len(password) just after password = input(&quot;Please enter your password for checking: &quot;).

huangapple
  • 本文由 发表于 2023年6月8日 12:49:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76428692.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定