[英]Read from top of text file that I use to store login username and password
My "user.txt" contains admin username and password(admin, admin).我的“user.txt”包含管理员用户名和密码(admin,admin)。 I use it to log in. Once I register a new user into the text file, the admin username and password don't work anymore, but I can log in using the new user credentials I have just created.
我用它来登录。一旦我在文本文件中注册了一个新用户,管理员用户名和密码就不再起作用,但我可以使用我刚刚创建的新用户凭据登录。 Please help.
请帮忙。
importing current date
from datetime import datetime
import csv
#opening user file
user_file = open("user.txt", "r+")
text = user_file.readlines()
login = False
#while login is false,
#if user enters correct credentials, login
#changes to True. Giving excess
while login == False:
username = input("Enter your username: ")
password = input("Enter your password: ")
for lines in text:
valid_user, valid_password = lines.split(", ")
valid_user, valid_password.strip()
if valid_user == username and valid_password == password:
login = True
print("Logging in...")
if valid_user != username and valid_password != password:
print("Invalid login details")
user_file.seek(0)
user_file.close()
At the end of the loop the valid_XXX
variables just contain the last line.在循环结束时,
valid_XXX
变量只包含最后一行。
You need to compare each line in the loop.您需要比较循环中的每一行。
for lines in text:
valid_user, valid_password = map(str.strip, lines.split(", "))
if valid_user == username and valid_password == password:
login = True
print("Logging in...")
break
else:
print("Invalid login details")
The else:
block of a loop is executed if the loop finishes normally instead of with a break
statement.如果循环正常结束而不是使用
break
语句,则执行else:
循环块。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.