[英]How do I loop my password/username login page and read the password/username from an external file?
我知道有關如何循環和讀取文本文件的多個帖子和資源。 我很抱歉成為那個人,但是我是Python的新手,我打算在凌晨1:00編寫這篇文章。
如標題所示,如何循環登錄頁面,以便如果用戶輸入錯誤的詳細信息,那么他們將有另一種嘗試機會,直到他們正確輸入了詳細信息。 密碼/用戶名也需要從外部文件中讀取。
我的代碼:
print ("\nEnter details to access wallet...\n")
username = 'Janupedia'
password = '12345'
userInput = input("What is your username?\n")
if userInput == username:
userInput = input("Password?\n")
if userInput == password:
print("Welcome!")
print('\n--------------------------------------------------------\n')
print ("BTN = 0.10")
print ("= £315.37")
else:
print("That is the wrong password.")
else:
print("That is the wrong username.")
print('\n--------------------------------------------------------\n')
做這樣的事情:
password = "password"
username = "username"
theirUsername = input("What is your username")
theirPassword = input("What is your password")
while theirUsername != username or theirPassword != password:
print("incorrect")
theirUsername = input("What is your username")
theirPassword = input("What is your password")
print("correct")
您可以使用file = open("externalfile.txt","r")
從外部文件讀取內容,然后執行text = file.read()
,如果文件的格式為
username
password
做text = text.split("\\n")
然后username = text[0]
和password = text[1]
這是它的樣子,並帶有一個解釋:
file = open("password.txt","r") #this opens the file and saves it to the variable file
text = file.read() #this reads what is in the file and saves it to the variable text
text = text.split("\n") #this makes the text into a list by splitting it at every enter
username = text[0] #this sets the username variable to the first item in the list (the first line in the file). Note that python starts counting at 0
password = text[1] #this sets the password variable to the second item in the list (the second line in the file)
theirUsername = input("What is your username") #gets username input
theirPassword = input("What is your password") #get password input
while theirUsername != username or theirPassword != password: #repeats the code inside while theirUsername is not equeal to username or theirPassword is not equal to password
print("incorrect") #notifies them of being wrong
theirUsername = input("What is your username") #gets new username input
theirPassword = input("What is your password") #gets new password input
print("correct") #tells them they are corrected after the looping is done and the password and username are correct
因此,您想循環播放。 一個好的地方在哪里? 當我們問一個問題時怎么樣。
現在,看看獲得正確的用戶名和密碼的情況。 我們不想在循環中處理它。 僅在此循環中獲取正確的用戶名和密碼。
print("\nEnter details to access wallet...\n")
username = "Janupedia"
password = "12345"
userInput = ""
while userInput != password:
userInput = input("What is your username?\n")
if userInput == username:
userInput = input("Password?\n")
if userInput == password:
break
else:
print("That is the wrong password.")
else:
print("That is the wrong username.")
print("Welcome!")
print("\n--------------------------------------------------------\n")
print("BTN = 0.10")
print("= £315.37")
todo_list = open("Credentials", "a")
todo_list.write("Username = Janupedia + Password = 12345")
todo_list.close()
print("\n--------------------------------------------------------\n")
現在從文件中讀取您的用戶名/密碼。 讓我們簡單點。 第一行是用戶名,第二行是密碼。 沒有其他項目。
現在創建一個適當的功能。
def read_credentials_from_file(filename):
"""Read the file and return (username, password).
File contents are first line username and second line password.
"""
# Using the `with` statement is current best practice.
with open(filepath, "rt") as user:
username = user.readline().strip()
password = user.readline().strip()
return username, password
現在修復您的代碼以使用該功能。
username, password = read_credentials_from_file(...)
請注意,在函數中我們去除了行尾。 如果使用的是Python 3.7,請使用breakpoint
函數單步執行代碼並觀察其作用。
假設您的文本文件(credentials.txt)讀為:
Janupedia
12345
也許這樣的事情將為您工作。 我評論了我添加的代碼。 您可能想要將憑證文件命名為其他名稱。
print ("\nEnter details to access wallet...\n")
"""
Open File
"""
with open("Credentials.txt", "r") as f:
array = []
for line in f:
array.append(line) #stores username and password
username = array[0]
password = array[1]
login = 0 #initial login status
while login == 0: #as long as login status = 0 loop repeats
userInput = input("Username?")
if username.strip(' \n') == userInput.strip(' \n'):
userInput = input("Password?")
if password.strip(' \n') == userInput.strip(' \n'):
login = 1 #login successful set login status to 1 thus breaking loop
else:
print("Incorrect")
else:
print("Incorrect")
print('\n--------------------------------------------------------\n')
# Login successful loop finished
print("Welcome!")
print('\n--------------------------------------------------------\n')
print ("BTN = 0.10")
print ("= 315.37")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.