我正在为一种计算机编写Python,并且登录模块总是返回true,即使它不在users
字典中:
cur_user = "null"
users = {
"Splavacado100": "20310"
}
def login():
good_login = 0
user_name = raw_input("Enter your username: ")
for user in users:
if user == user_name:
good_login = 1
if good_login == 1:
user_password = raw_input("Enter your password: ")
if users[user_name] == user_password:
return True
else:
return False
else:
return "None"
def OS():
log_att = False
log_att = login()
if log_att:
print "True"
else:
print "False"
OS()
在这些情况下如何让它返回false?
我正在使用Python 2.7.10。
答案 0 :(得分:3)
不返回“None”,而是返回None(因为“None”始终为true,因为它是非零值)。
答案 1 :(得分:0)
你的第二种方法有问题。我拿出来了,第一部分有效。然后,我在第一种方法中取出了最后一个,似乎工作。试试this
cur_user = "null"
users = {
"Splavacado100": "20310"
}
def login():
good_login = 0
user_name = raw_input("Enter your username: ")
for user in users:
if user == user_name:
good_login = 1
if good_login == 1:
user_password = raw_input("Enter your password: ")
if users[user_name] == user_password:
return True
else:
return False
def OS():
log_att = False
log_att = login()
if log_att:
print "True"
else:
print "False"
OS()
答案 2 :(得分:0)
我认为您的登录功能应该更像;
def login():
raw_user = raw_input("Enter your username: ")
password = users.get(raw_user)
if not password:
return False
raw_password = raw_input("Enter your password: ")
if password == raw_password:
return True
return False
然后你的第二个功能可以简化:
def OS():
if login():
print "True"
else:
print "False"