此任務的目的是檢查 user.txt 檔案中的每一行(包含用戶名和密碼串列)
如果沒有找到任何匹配的用戶名和密碼,程式應該將輸入的用戶名和密碼與 txt 檔案中的每一行進行比較
user.txt
Steve steve123
Eddie ed123
Dexter dex123
我幾乎嘗試了我能想到的任何東西。
盡管我幾乎可以肯定問題出在回圈中。
好像只比較txt檔案的最后一行
我對此還是很陌生,所以請憐憫:)
關于如何讓程式檢查用戶輸入是否與每一行文本匹配的任何提示。此外,txt 檔案可以隨時更改或添加,如果不是,我可以使用行索引來完成此操作。
content = ""
user_List = []
while True:
print("Username")
username = input()
print("Password")
password = input()
name_pass = username " " password
with open("user.txt" , "r ") as f:
for line in f:
if name_pass == line:
x = False
else:
x = True
if x == False:
print("Wellcome")
break
elif x == True:
print("Incorrect Username or Password \nPlease try again")
uj5u.com熱心網友回復:
問題必須是,當您遍歷該行時,該行將以 \n 結尾,因為在您的 txt 檔案中有一個嵌入的回傳,而您沒有看到 \n!. 為了驗證這一點,您可以在 ? for line in f ? 陳述句之后添加此陳述句 ? print(repr(line)) ?。repr 函式(representation) 將向您顯示字串而不去掉 \n。如果是這種情況,您只需像這樣驗證:如果 name_pass == line[:-1],我很確定您理解最后一條陳述句啊哈;)
uj5u.com熱心網友回復:
我改變了變數x,現在它被稱為deny_access. 無論如何,我認為問題出在 for 回圈中。在您的代碼中,您修改每一行中的變數x,因此在 for 回圈之后唯一重要的行是最后一行。這是錯誤的。以下是修復代碼的方法:
content = ""
user_List = []
while True:
print("Username")
username = input()
print("Password")
password = input()
name_pass = username " " password
with open("user.txt" , "r ") as f:
deny_access = True
for line in f:
if name_pass == line:
deny_access = False
break
if deny_access == False:
print("Wellcome")
break
elif deny_access == True:
print("Incorrect Username or Password \nPlease try again")
注意:使用此修復程式,它應該可以作業。但這并不意味著它是安全的,也不意味著它是一種好的編程習慣。我的意思是,如果這是您正在測驗要學習的東西的代碼或一個小專案,那么它是完美的。但如果它用于生產或更大的東西,你必須改變一堆東西。
uj5u.com熱心網友回復:
文本檔案中所有每一行的第一行都以新行結尾,因此要匹配 name_pass 變數,您需要在其末尾添加新行
name_pass = username " " password "\n"
其次,如果陳述句的邏輯不正確,您需要在找到匹配項后中斷回圈,這樣 x 就不會再次被錯誤值覆寫
with open("user.txt", "r ") as f:
for line in f:
if name_pass == line:
x = False
break
else:
x = True
uj5u.com熱心網友回復:
你來了,伙計。嘗試這個。在這種情況下,我總是這樣做。如果您嘗試模擬登錄頁面之類的內容,可以使用此代碼作為示例。您還可以看到在類似情況下使用文本檔案保存密碼的示例。
import os
import shutil
def action():
action = int(input("what you want to do?\n1 for create account\n2 for delete existed account\n: "))
if action == 1:
signup()
elif action == 2:
delete_account()
else:
print("no valid option has been selected")
def delete_account():
username = input("enter your username: ")
password = input("enter your password: ")
if os.path.isdir(f"./{username}_account"):
with open(f"./{username}_account/password", 'r') as passfile:
pass_containment = passfile.read()
if password == pass_containment:
shutil.rmtree(f"./{username}_account")
print("your account has been removed")
else:
print("username or password is not correct")
else:
print("username or password is not correct")
def signup():
username = input("set a username: ")
password = input("set a password: ")
conf_password = input("confirm your password: ")
if username == password:
print("password and username cannot be same")
else:
if os.path.isdir(f"./{username}_account"):
print("account has been already existed, choose another username")
else:
if password == conf_password:
os.mkdir(f"{username}_account")
with open(f"./{username}_account/username", 'w') as userfile:
userfile.write(username)
with open(f"./{username}_account/password", 'w') as passfile:
passfile.write(str(password))
else:
print("passwords are not same")
action()
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/452303.html
