时间:2019-02-13 12:28:22

标签: python

我遇到了一个问题,使我无法创建登录系统。 我要在这里做的是打开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文件行。 enter image description here

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

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;

ERROR2

1 个答案:

答案 0 :(得分:0)

这不起作用,使用上下文管理器的正确方法是使用nil关键字,而不是with。使用上下文管理器后,还要保留正确的缩进。

相关问题