Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python登录恢复系统存在问题_Python_Python 3.x - Fatal编程技术网

Python登录恢复系统存在问题

Python登录恢复系统存在问题,python,python-3.x,Python,Python 3.x,这是一个用户可以恢复其用户名或密码的系统 account = input("What do you want to recover? Username or Password? ") if account == ("Password") or account == ("password"): check = True while check: username = input("Enter your username for your account ")

这是一个用户可以恢复其用户名或密码的系统

account = input("What do you want to recover? Username or Password? ")
if account == ("Password") or account == ("password"):
    check = True
    while check:
        username = input("Enter your username for your account ")
        with open("accountfile.txt","r") as file:
            for line in file:
                text = line.strip().split()
                if username in text:
                    print(line)
                    check = False
                else:
                    print("Username not found")

文本文件中的格式是:
username:(username)password:(password)
出于某种原因,当我输入帐户的用户名时,它会给出它的密码,但出于某种原因,它在结尾说
找不到username
,我不知道如何解决这个问题。

检查=False
之后,您必须添加
break
。这是因为你的循环在每一行都在继续,导致“找不到用户名”打印。另外,由于
check
变为
False
,我们可以在循环完成后检查此项。守则是:

account = input("What do you want to recover? Username or Password? ")
if account == ("Password") or account == ("password"):
    check = True
    while check:
        username = input("Enter your username for your account ")
        with open("accountfile.txt","r") as file:
            for line in file:
                text = line.strip().split()
                if username in text:
                    print(line)
                    check = False
                    break
            if (check == True):
                print("Username not found")
结果:

输入:

您需要退出for循环。它打印用户名,然后进行下一次评估,然后用户名不存在,所以它打印找不到的用户名。这不是问题吗?在check=false之后输入“break”这只是部分答案。脚本将一直打印“Username not found”,直到找到匹配项为止–也就是说,如果用户名位于第42行,您将看到41行显示“Username not found”@JJJ是的,你是对的,我将其他用户放入该文件,并对其进行了测试sthttps://stackoverflow.com/questions/54062441/problem-with-python-login-recovery-system/54063989#comment94963389_54063989ill sayes
未找到用户名
您知道永久性修复吗?请选中编辑。刚刚修好了@python987还添加了输入和结果。