第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何將多行文本文件分解為一個(gè)列表,該列表可以迭代以檢查 python 中是否輸入 == 文本文件?

如何將多行文本文件分解為一個(gè)列表,該列表可以迭代以檢查 python 中是否輸入 == 文本文件?

紅顏莎娜 2023-06-20 16:41:52
運(yùn)行代碼時(shí)出現(xiàn)邏輯錯(cuò)誤,它繼續(xù)拋出第一個(gè) if 語句錯(cuò)誤“錯(cuò)誤!用戶名不存在?!?我需要能夠以管理員身份登錄,然后通過將用戶添加到 .txt 文件來添加用戶,之后,如果程序再次運(yùn)行,我可以通過管理員或在 txt 中創(chuàng)建的新用戶之一登錄文件。我似乎無法正確拆分它,以便循環(huán)在登錄時(shí)正確地遍歷列表。例子:print(new_lines) = [['admin', 'adm1n'], ['kevin', 'kev1n'], ['dorothy', '1234']].txt 文件內(nèi)容,每個(gè)條目在新行 = admin,adm1n\n kevin,kev1n\n dorothy,1234到目前為止的代碼:import time#User input of username and passworduser_name = input("Username:\n")user_pass = input("Password: \n")#Opening documentwith open("user.txt", "r+", encoding = "utf-8-sig") as f:        new_lines = []    for line in f:        new_line = line.strip()        new_lines.append(new_line.split(","))            print(new_lines)    #Loop to enter user name and password    for x in new_lines:        for y in x:            if user_name != new_lines[:][0]:                print("Error! Username does not exist.")                user_name = input("Username:\n")                user_pass = input("Password: \n")                            elif user_pass != new_lines[:][1]:                print("Error! Incorrect password.")                user_name = input("Username:\n")                user_pass = input("Password: \n")                            else:                print("Welcome back!")                break        break            #User options to choose from            user_choice = input("""\nPlease select one of the following options:                            \nr - register user                            \na - add task                             \nva - view all tasks                            \nvm - view my tasks                            \ne - exit                            \nAnswer: """)
查看完整描述

2 回答

?
qq_遁去的一_1

TA貢獻(xiàn)1725條經(jīng)驗(yàn) 獲得超8個(gè)贊

我建議稍微重新格式化代碼,以便更容易找到用戶名是否存在以及密碼是否正確。


import time


#User input of username and password


user_name = input("Username:\n")

user_pass = input("Password: \n")


#Opening document


with open("user.txt", "r+", encoding = "utf-8-sig") as f:

    

    new_lines = []

    for line in f:

        new_line = line.strip()

        new_lines.append(new_line.split(","))


usernames = [acc[0] for acc in new_lines]

pws = [acc[1] for acc in new_lines]


while True:

    if user_name not in usernames:

        print("Error! Username does not exist.")

        user_name = input("Username:\n")

        user_pass = input("Password: \n")

    else:

        pw_index = usernames.index(user_name)

        if user_pass != pws[pw_index]:

            print("Error! Incorrect password.")

            user_name = input("Username:\n")

            user_pass = input("Password: \n")

        else:

            print("Welcome back!")

            break


#User options to choose from        

user_choice = input("""\nPlease select one of the following options:

                        \nr - register user

                        \na - add task 

                        \nva - view all tasks

                        \nvm - view my tasks

                        \ne - exit

                        \nAnswer: """)


查看完整回答
反對(duì) 回復(fù) 2023-06-20
?
慕妹3146593

TA貢獻(xiàn)1820條經(jīng)驗(yàn) 獲得超9個(gè)贊

你在循環(huán)中有一個(gè)邏輯錯(cuò)誤 - 你似乎將文本文件中的每一行與提供的用戶名和密碼進(jìn)行比較,如果它們不匹配則說明錯(cuò)誤 - 忽略用戶可能不在第一行的事實(shí)文件。僅當(dāng)您遍歷整個(gè)文件但未找到用戶,或找到用戶但密碼不匹配時(shí),才會(huì)顯示該錯(cuò)誤。


另外,我認(rèn)為您根本不需要內(nèi)部循環(huán),您聲明了 x 和 y 但不使用它們,您是否嘗試過打印它們并檢查它們包含的內(nèi)容?


無論如何,這是我認(rèn)為循環(huán)應(yīng)該是什么樣子的輪廓


found = False

for current_user, current_password in new_lines:

    if current_user == user_name:

        if current_password == user_pass:

            print("Welcome back!")

            found = True

        else:

            print("Error! Incorrect password")

       break

if not found:

    print("Error! Username does not exist.")


查看完整回答
反對(duì) 回復(fù) 2023-06-20
  • 2 回答
  • 0 關(guān)注
  • 160 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)