如何解决使用 .txt 文件制作登录身份验证程序以保存用户详细信息,但出现 ValueError: not enough values to unpack (expected 2, got 1)
granted = False
def grant():
global granted
granted = True
参数中的登录名和密码,用于检查用户是否已被占用。
def login(name,password):
success = False
file = open("data.txt","r")
for i in file:
a,b = i.split(",")
b = b.strip()
if (a==name and b==password):
success = True
break
file.close()
if(success):
print("Login successful!!!")
grant()
else:
print("Wrong credentials. Please try again.")
用于向 data.txt 文件中添加新用户的注册函数。
def register(name,password):
file = open("data.txt","a")
file.write("\n"+name+","+password)
file.close
grant()
这用于在验证所有内容后让用户访问程序的其余部分。
def access(option):
global name
if(option == "login"):
name = input("Enter your name: ")
password = input("Enter your password: ")
login(name,password)
else:
print("Enter your name and password to register.")
name = input("Enter your name: ")
password = input("Enter your password: ")
register(name,password)
程序运行时首先显示的介绍部分,用于检查用户是否 想登录或注册。
def begin():
global option
print("Welcome.")
option = input("Login or Register (login,reg): ")
if(option != "login" and option != "reg"):
begin()
begin()
access(option)
if(granted):
print("Welcome to the programming club")
print("### User details ###")
print("Username: ",name)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。