我不擅长编程,所以这就是为什么我在这里寻求帮助。我正在为我的酒店预订系统创建一个登录系统,但我遇到了一些问题。这是我通过帮助别人的代码为登录系统所做的。链接:Python Login and Register System using text files
def get_existing_users():
with open("login.txt", "r" ) as f:
for line in f.readlines():
# This expects each line of a file to be (name, pass) seperated by whitespace
username, password = line.split(',')
yield username, password
def is_authorized(username, password):
return any((user == (username, password) for user in get_existing_users()))
def user_exists(username):
return any((usr_name == username) for usr_name, _ in get_existing_users())
# above is equivalent of:
#
# for usr_name, _ in get_existing_users():
# if usr_name == username:
# return True
# return False
def ask_user_credentials():
count = 0
while count < 3:
username = input("Enter Username: ")
password = input("Enter a Password: ")
if is_authorized(username, password):
print ("Welcome to the Majestic Hotel Booking system ") + username
MainMenu()
if user_exists(username):
print("The password entered is wrong, please try again")
print("The username entered is wrong, please try again")
count = count + 1
def getdetails():
forename = input("Enter forename: ")
surname = input("Enter Surname: ")
year = input("Enter year you are born: ")
if year <= '2004':
print("You are old enough to register yourself")
username = forename[0]+surname[0:3]+year[3:4]
print("Username: ", username)
password = input("Enter a password: ")
file = open("login.txt","a")
file.write(username + "," + password + "\n")
file.close()
MainMenu()
else:
print("Sorry you are not old enough to register by yourself") enter code here
if account == "No":
print(getdetails())
if account == "Yes":
print(ask_user_credentials())
创建或登录帐户后,程序会将它们定向到主菜单,这就是为什么您会在代码中看到 MainMenu()。
我创建了一个帐户,如下所示:
WELCOME TO THE MAJESTIC HOTEL lOGIN SYSTEM
************************************************
Do you have an account? Yes or No: No
Enter forename: Rose
Enter Surname: Moon
Enter year you are born: 2000
You are old enough to register yourself
Username: RMoo0
Enter a password: door
然后将其保存在名为 login.txt 的文本文件中,如下所示:
RMoo0,door
然而,虽然我创建了一个帐户,它被保存到文本文件,但输出是这样说的:
WELCOME TO THE MAJESTIC HOTEL lOGIN SYSTEM
************************************************
Do you have an account? Yes or No: Yes
Enter Username: RMoo0
Enter a Password: door
The password entered is wrong, please try again
The username entered is wrong, please try again
Enter Username:
有人可以帮助我,并以一种我会理解的方式解释解决方案。
f.readlines()给出 txt 文件的每一行:
line1\n
line2\n
.
.
.
因此,当您尝试通过拆分(“,”)获取用户名,密码时,它看起来像这样
("user1", "password1\n")
("user2", "password2\n")
..
在使用“”拆分行之前,请使用rstrip ("\ n ")右剥离每行
def get_existing_users():
with open("login.txt", "r" ) as f:
for line in f.readlines():
username, password = line.rstrip("\n").split(',') #used rstrip here to remove the right ending "\n"
yield username, password
在解决这类问题时,您应该考虑几个方面,例如转义您的分隔符(当然,如果您对密码进行哈希处理,则没有必要)并且不使用空格,一个非常好的方法,如 linux passwd 文件是在行尾添加分隔符,以避免不同的 EOL 终止符,如\ r,\ n 或\ r\ n 例如:
def get_existing_users():
"""
This expects each line of a file to be:
(start of line)name,pass,(end of line)
"""
with open("login.txt", "r" ) as f:
for line in f.readlines():
user = line.split(',')
yield user[0], user[1]
本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处
评论列表(9条)