Python 如何搜索文件&;检查是否有
我要一个用户登录系统。请说明我是否为我的文本文件(数据库)声明了一个变量 这是说,都登录到帐户,也很抱歉找不到一个帐户Python 如何搜索文件&;检查是否有,python,python-3.x,list,Python,Python 3.x,List,我要一个用户登录系统。请说明我是否为我的文本文件(数据库)声明了一个变量 这是说,都登录到帐户,也很抱歉找不到一个帐户 if l_username or l_password in data.read(): print("Congrats! You are successfully now logged into your account!") if l_username or l_password not in data.read():
if l_username or l_password in data.read():
print("Congrats! You are successfully now logged into your account!")
if l_username or l_password not in data.read():
error = str(input(
"Sorry we couldn't find your account. "
"\nEnter the correct username or password."
"\n\tor \nType 'r' to retry \nor \nCreate one by typing 'c'\n:"))
我的文本文件(其中存储所有帐户,变量为该文件的数据)
请告诉我是否为该文件声明了变量
我的文本文件包含了所有这些。是这样吗
Username: apple
Password: apples
这是因为您在第一部分中使用了“或”
if l_username and l_password in data.read():
print("Congrats! You are successfully now logged into your account!")
请像我上面所做的那样,将
或“
替换为和“
”,并告诉我这是否有效。(保留第二部分中的或
,只需将此特定部分更改为和“
)这里显然存在一个问题:
if l_username or l_password in data.read():
print("Congrats! You are successfully now logged into your account!")
if l_username or l_password not in data.read():
error = str(input(
"Sorry we couldn't find your account. "
"\nEnter the correct username or password."
"\n\tor \nType 'r' to retry \nor \nCreate one by typing 'c'\n:"))
登录是通过用户名和密码进行的,因此在代码中,顶部的或
操作符意味着,如果用户名或密码正确,您就可以登录。
两个输出都是由于密码或用户名正确,另一个不正确
最终解决方案是什么?只需将第一条语句中的或替换为和,尝试使用:
line=data.read()
如果行中有l_用户名和l_密码:
打印(“恭喜!您现在已成功登录到您的帐户!”)
l_username
中的值有效;在这种情况下,情况总是如此。您只是将l_密码
与data.read()中的读取行进行比较。这就是为什么你会收到这两条信息
您读取文件的方法可能会有问题。我建议您在本地变量中维护数据,或者在每次登录时读取整个文件
这里的另一个关键点是,与data.read()
相比,您在l\u用户名
和l\u密码
方面没有什么不同。如果用户在密码中插入登录值,它也将是真的。如果您需要一种简单的方法将数据存储在基于文本的文件中并使其易于读取,您可能需要查看json
。有非常强大的Python函数用于读取和写入这些文件。你可以找到更多信息
if l_username or l_password in data.read():
print("Congrats! You are successfully now logged into your account!")
if l_username or l_password not in data.read():
error = str(input(
"Sorry we couldn't find your account. "
"\nEnter the correct username or password."
"\n\tor \nType 'r' to retry \nor \nCreate one by typing 'c'\n:"))