我试图做一个基本的登录系统,但我有一些逻辑问题。
我想在没有填写电子邮件或密码字段时显示“登录不正确”消息,并且当结果不包含通知电子邮件和密码的用户时也是如此。
但是当电子邮件或密码字段未填写时,它没有显示“登录不正确”消息
你可以帮忙让这个工作正常吗?
def login():
while True:
email = ""
password = ""
while not email or not password:
email = raw_input("Enter your username:")
password = raw_input('Password:')
results = # get a row from database where email = email and password = password
for result in results:
if result["email"] == email and result["password"] == password:
print "Login correct"
return True
else:
return False
def main():
if login():
# do something
else:
print "Login is incorrect"
main()
答案 0 :(得分:1)
def login():
while True:
email = ""
password = ""
while not email or not password:
email = raw_input("Enter your username:")
password = raw_input('Password:')
results = # get a row from database where email = email and password = password
for result in results:
if result["email"] == email and result["password"] == password:
print "Login correct"
return True
else:
print "Login incorrect"
return False
答案 1 :(得分:1)
问题在于登录方式:
def login():
while True:
email = ""
password = ""
while not email or not password:
email = raw_input("Enter your username:")
password = raw_input('Password:')
if not email or not password:
return False #this is the key
results = # get a row from database where email = email and password = password
for result in results:
if result["email"] == email and result["password"] == password:
print "Login correct"
return True
else:
return False
def main():
if login():
# do something
else:
print "Login is incorrect"
main()
答案 2 :(得分:1)
您的代码中存在逻辑错误。
您要求您的数据库为您提供具有匹配用户和密码的所有结果。结果应该是完全一行或没有行。
如果你有一行,那一定是正确的。无需再次比较用户名和密码,因为您在数据库查询中执行了此操作。
如果数据库查询产生没有行,您的代码将不会进入for循环,因此不会打印任何内容。