繁体   English   中英

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

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

我有一个问题,不允许我制作登录系统。 我想在这里做的是打开TXT文件,并在一行中搜索用户名和密码。 如果密码和用户名在一行,则登录成功,我们可以继续。 我的代码:

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;

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

编辑** 编辑一些代码后,我又收到一个错误。

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

它不应该工作,使用上下文管理器的正确方法是使用with关键字, while不是while 使用上下文管理器后还要保留正确的缩进。

暂无
暂无

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

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