简体   繁体   English

在 TXT 文件中读取行的问题,对于行输入

[英]Problem with reading lines in TXT File, for line in

I have a problem which don't let me to make a Login System.我有一个问题,不允许我制作登录系统。 What I want to do here is Open the TXT file, and search for Username and Password in one line.我想在这里做的是打开TXT文件,并在一行中搜索用户名和密码。 If the password and Username is in the one Line, the login is successful, we can continue.如果密码和用户名在一行,则登录成功,我们可以继续。 My code:我的代码:

elif "l" in Register_Login or "L" in Register_Login:
        check = True
        while open(Database, mode = 'r', encoding = 'utf-8') as f:
        Vardas = input("Please enter your Username!\n")
        Password = getpass.getpass("Please enter your Password!\n")
        for line in f:
            if("Vardas : " + Vardas + " Password : " + Password + " ") == line.strip():
                print("You're logged in")
                check = False
                break;
            else:
                print("Wrong Combination!")
                continue;

My TXT File Lines.我的 TXT 文件行。 在此处输入图片说明

EDIT** After editing some code, I get another error.编辑** 编辑一些代码后,我又收到一个错误。

elif "l" in Register_Login or "L" in Register_Login:
    check = True
    with open('Registruoti.txt', mode = 'r', encoding = 'utf-8') as f:
        Vardas = input("Please enter your Username!\n")
        Password = getpass.getpass("Please enter your Password!\n")
    for line in f:
        if("Vardas : " + Vardas + " Password : " + Password + " ") == line.strip():
            print("You're logged in")
            check = False
            break;
        else:
            print("Wrong Combination!")
            continue;

错误2

It is not supposed to work, the correct way to use a context manager is by using with keyword, not while .它不应该工作,使用上下文管理器的正确方法是使用with关键字, while不是while Also preserve the correct indentation after using a context manager.使用上下文管理器后还要保留正确的缩进。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM