我知道有关如何循环和读取文本文件的多个帖子和资源。我很抱歉成为那个人,但是我是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')
答案 0 :(得分:0)
做这样的事情:
data <-
structure(list(QuestionNumber = structure(c(17L, 17L, 17L, 17L,
17L, 17L, 17L, 17L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 15L, 15L,
15L, 15L, 15L), .Label = c("Q17", "Q16", "Q15", "Q14", "Q13",
"Q12", "Q11", "Q10", "Q9", "Q8", "Q7", "Q6", "Q5", "Q4", "Q3",
"Q2", "Q1"), class = c("ordered", "factor")), stemmed = structure(c(2L,
1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 1L, 1L, 1L, 1L, 3L, 1L, 2L, 2L,
3L, 2L, 3L), .Label = c("No", "Yes", "Maybe"), class = "factor")), row.names = c(NA,
20L), class = "data.frame")
您可以使用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]
答案 1 :(得分:0)
因此您想循环播放。一个好的地方在哪里?我们问一个问题怎么样?
现在,看看获得正确的用户名和密码的情况。我们不想在循环中处理它。只有在循环中才能获取正确的用户名和密码。
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
函数单步执行代码并观察其作用。
答案 2 :(得分:0)
假设您的文本文件(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")