具有功能的登录脚本不会对用户进行身份验证
Login Script with Function Won't Authenticate User
我创建了一个使用名为 "authenticateuser" 的函数的脚本,当我输入错误的用户名和密码时,该程序可以正常运行,但是当我输入正确的凭据时,它仍然 returns 失败.尝试四处移动但找不到解决方案。是位置不对还是我遗漏了一些最终代码?
loggedin = False
wrongcount = 0
def authenticateuser(theusername, thepassword):
theusername = "homerjsimpson"
thepassword = "marge"
def main():
username = ""
password = ""
while loggedin == False and wrongcount < 5:
username = input("Please enter username: ")
password = input("Please enter password: ")
if password == authenticateuser and username == authenticateuser:
loggedin = True
else:
print("Authentication Failed")
wrongcount = wrongcount + 1
loggedin = False
if(loggedin == True):
print("Welcome to the program!")
else:
print("Locked Out")
main()
您正在检查密码和用户名是否是函数,显然它们不是。我相信您实际上想要 authenticateuser
到 return 包含 theusername
和 thepassword
的字典。像这样:
def authenticate_user(username, password):
return {"username": username, "password": password}
...
credentials = authenticate_user("homerjsimpson", "marge")
while logged_in == False and wrong_count < 5:
username = input("Please enter username: ")
password = input("Please enter password: ")
if password == credentials["password"] and username == credentials["username"]:
logged_in = True
else:
print("Authentication Failed")
wrong_count = wrong_count + 1
loggedin = False
(作为旁注,您应该使用 _
来分隔变量和函数名称中的单词)
您没有正确调用 authenticateuser。像这样的事情会如你所愿:
loggedin = False
wrongcount = 0
def authenticateuser(theusername, thepassword):
if theusername == "homerjsimpson" and thepassword == "marge":
return True
else:
return False
def main():
username = ""
password = ""
while loggedin == False and wrongcount < 5:
username = input("Please enter username: ")
password = input("Please enter password: ")
if authenticateuser(username, password):
loggedin = True
else:
print("Authentication Failed")
wrongcount = wrongcount + 1
loggedin = False
if(loggedin == True):
print("Welcome to the program!")
else:
print("Locked Out")
main()
编辑:此外,您的 main 调用没有执行任何操作。 python 代码逐行评估,因此您的 "main" 循环实际上是您执行 "while loggedin == False" 的地方。当你调用函数 main 时,你的程序基本上已经完成了所有事情,main 只是将用户名和密码设置为空字符串,但对这些值没有做任何进一步的事情
authenticateuser
必须对输入参数做一些处理,而 return True
如果 username/passord 匹配,否则它必须 return False。
我们可以用很多不同的方式来写,例如版本 1:
def authenticateuser(theusername, thepassword):
if theusername == "homerjsimpson" and thepassword == "marge":
return True
else:
return False
版本 2(更好):
def authenticateuser(theusername, thepassword):
return theusername == "homerjsimpson" and thepassword == "marge"
版本 3(甚至更好):
def authenticateuser(theusername, thepassword):
authentication_db = {
# username # password
'homerjsimpson': 'marge',
}
return authentication_db.get(theusername) == thepassword
通常当我们让某人登录时,我们需要跟踪他们的登录状态。让我们创建一个简单的 class(为此目的 Session
):
class Session:
def __init__(self, username=None, loggedin=False):
self.username = username
self.loggedin = loggedin
登录功能现在可以询问用户名和密码,并调用 authenticateuser
查看它们是否正确。如果它们不正确,我们会增加 wrongcount 计数器。
在任何一种情况下,我们 return 包含用户名和用户是否登录的会话:
def login():
loggedin = False
wrongcount = 0
while not loggedin:
username = input("Please enter username: ")
password = input("Please enter password: ")
if authenticateuser(username, password):
return Session(username, True)
wrongcount += 1
if wrongcount > 5:
return Session(username, False)
现在 main 可以调用 login()
并取回 session
对象。可以检查此对象的 .loggedin
并打印相应的消息。由于我们已经记录了用户名,我们还可以个性化消息:
def main():
session = login()
if session.loggedin:
print("Welcome to the program!", session.username)
else:
print(session.username, "you've been locked out!")
main()
我创建了一个使用名为 "authenticateuser" 的函数的脚本,当我输入错误的用户名和密码时,该程序可以正常运行,但是当我输入正确的凭据时,它仍然 returns 失败.尝试四处移动但找不到解决方案。是位置不对还是我遗漏了一些最终代码?
loggedin = False
wrongcount = 0
def authenticateuser(theusername, thepassword):
theusername = "homerjsimpson"
thepassword = "marge"
def main():
username = ""
password = ""
while loggedin == False and wrongcount < 5:
username = input("Please enter username: ")
password = input("Please enter password: ")
if password == authenticateuser and username == authenticateuser:
loggedin = True
else:
print("Authentication Failed")
wrongcount = wrongcount + 1
loggedin = False
if(loggedin == True):
print("Welcome to the program!")
else:
print("Locked Out")
main()
您正在检查密码和用户名是否是函数,显然它们不是。我相信您实际上想要 authenticateuser
到 return 包含 theusername
和 thepassword
的字典。像这样:
def authenticate_user(username, password):
return {"username": username, "password": password}
...
credentials = authenticate_user("homerjsimpson", "marge")
while logged_in == False and wrong_count < 5:
username = input("Please enter username: ")
password = input("Please enter password: ")
if password == credentials["password"] and username == credentials["username"]:
logged_in = True
else:
print("Authentication Failed")
wrong_count = wrong_count + 1
loggedin = False
(作为旁注,您应该使用 _
来分隔变量和函数名称中的单词)
您没有正确调用 authenticateuser。像这样的事情会如你所愿:
loggedin = False
wrongcount = 0
def authenticateuser(theusername, thepassword):
if theusername == "homerjsimpson" and thepassword == "marge":
return True
else:
return False
def main():
username = ""
password = ""
while loggedin == False and wrongcount < 5:
username = input("Please enter username: ")
password = input("Please enter password: ")
if authenticateuser(username, password):
loggedin = True
else:
print("Authentication Failed")
wrongcount = wrongcount + 1
loggedin = False
if(loggedin == True):
print("Welcome to the program!")
else:
print("Locked Out")
main()
编辑:此外,您的 main 调用没有执行任何操作。 python 代码逐行评估,因此您的 "main" 循环实际上是您执行 "while loggedin == False" 的地方。当你调用函数 main 时,你的程序基本上已经完成了所有事情,main 只是将用户名和密码设置为空字符串,但对这些值没有做任何进一步的事情
authenticateuser
必须对输入参数做一些处理,而 return True
如果 username/passord 匹配,否则它必须 return False。
我们可以用很多不同的方式来写,例如版本 1:
def authenticateuser(theusername, thepassword):
if theusername == "homerjsimpson" and thepassword == "marge":
return True
else:
return False
版本 2(更好):
def authenticateuser(theusername, thepassword):
return theusername == "homerjsimpson" and thepassword == "marge"
版本 3(甚至更好):
def authenticateuser(theusername, thepassword):
authentication_db = {
# username # password
'homerjsimpson': 'marge',
}
return authentication_db.get(theusername) == thepassword
通常当我们让某人登录时,我们需要跟踪他们的登录状态。让我们创建一个简单的 class(为此目的 Session
):
class Session:
def __init__(self, username=None, loggedin=False):
self.username = username
self.loggedin = loggedin
登录功能现在可以询问用户名和密码,并调用 authenticateuser
查看它们是否正确。如果它们不正确,我们会增加 wrongcount 计数器。
在任何一种情况下,我们 return 包含用户名和用户是否登录的会话:
def login():
loggedin = False
wrongcount = 0
while not loggedin:
username = input("Please enter username: ")
password = input("Please enter password: ")
if authenticateuser(username, password):
return Session(username, True)
wrongcount += 1
if wrongcount > 5:
return Session(username, False)
现在 main 可以调用 login()
并取回 session
对象。可以检查此对象的 .loggedin
并打印相应的消息。由于我们已经记录了用户名,我们还可以个性化消息:
def main():
session = login()
if session.loggedin:
print("Welcome to the program!", session.username)
else:
print(session.username, "you've been locked out!")
main()