嗨,我完全不知道如何做我想做的事,我基本上是在检查一个文件,其中包含两个单词,例如用户的用户名和密码。
text = open("Accounts.dat", 'w')
text.writelines("the username")
text.writelines("\n")
text.writelines("the password")
text.close()
username = input("Enter username: ")
password = input("Enter password: ")
data = open("Accounts.dat").read()
if username and password in data:
print("works")
else:
print("doesn't work")
data.close()
这段代码确实在某些方面起作用,比如如果我只输入正确的用户名,而没有输入任何密码,它就会打印(“有效”),但如果我在用户名和密码中输入任何内容,它就会打印(“不起作用”) ,那么如果我只输入正确的密码而不输入任何用户名,则不起作用。
我需要它,因此只有当用户名和密码正确时才会打印(“有效”)。
最佳答案
如果数据中的用户名和密码:
应该是
如果数据中包含用户名,数据中包含密码:
当你执行if username and password in data:
时,python 解释它的方式是:
if (username) and (password in data):
也就是说,检查用户名
是否为True
并检查密码
是否在数据
中
关于python 'and' boolean 运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16575250/