【发布时间】:2013-05-15 21:27:57
【问题描述】:
嗨,我对如何做我想做的事感到完全困惑,我基本上是在检查一个文件,其中包含两个单词,例如用户的用户名和密码。
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()
此代码在某些方面确实有效,例如,如果我只输入正确的用户名,而它打印的密码没有任何内容(“有效”),但如果我在用户名和密码中没有输入任何内容,它就会打印(“不起作用”) ,那么如果我只输入正确的密码,而没有输入用户名,它就不起作用了。
我需要它,所以它只在用户名和密码都正确时打印(“有效”)。
【问题讨论】:
-
这不是一个好的方案。例如,您可以再次输入用户名作为密码。
标签: python boolean and-operator