在python中生成登录函数时出现问题



你好,我是一名学生,我在这里研究了太久,找不到如何让这个登录页面真正工作的答案。我找到的所有页面都以不同的方式制作,但对我来说不起作用。有人能把我推到正确的轨道上吗。不过我的注册页面已经开始工作了。

def signup():

users = open("login_signup.txt","a")
user = []
username = input("What do you want your username to be?: ")
password = input("What do you want your password to be?: ")
user.append(username)
user.append(password)
users.write(username + "," + password)
users.write("n")
print("You have successfully signed up")

def login():

with open("login_signup.txt","r") as users:

usersusername = input("What is your username?: ")
userspassword = input("What is your password?: ")

顺便说一句,文件中文本的格式是:用户名,密码

然后,在下一个人想要创建帐户后,它转到一个新行。

感谢任何可以提供帮助的人:(

由于您仍然有问题,因此对代码进行了修改。

def signup():

with open("login_signup.txt","a") as users: # context manager is preferred over pure open/close
#user = []                              # not used so remove
username = input("What do you want your username to be?: ")
password = input("What do you want your password to be?: ")
#user.append(username)                  # not used so remove
#user.append(password)                  # not used so remove
users.write(username + "," + password)
users.write("n")
print("You have successfully signed up")

def login():

usersname = input("What is your username?: ")
userspassword = input("What is your password?: ")

with open("login_signup.txt", "r") as users:
for line in users:                              # iterating over login file a line at a time
login_info = line.rstrip().split(',')       # rstrip() removes the 'n' at end of string
# split(',' split string on comma 
if usersname == login_info[0] and userspassword == login_info[1]:
print("Correct credentials!")
return True

print("Incorrect credentials.")
return False

Exmaple Run

sigup()
# Out:
#     What do you want your username to be?: john
#     What do you want your password to be?: paul
#     You have successfully signed up

login()
# Out:
#    What is your username?: john
#    What is your password?: paul
#    Correct credentials!
#    True

相关内容

  • 没有找到相关文章

最新更新