簡體   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