带功能的登录脚本无法验证用户身份

时间:2017-03-11 00:01:26

标签: python function python-3.x login-script

我创建了一个使用名为“authenticateuser”的函数的脚本,当我输入错误的用户名和密码时程序正常工作,但是当我输入正确的凭据时它仍然返回失败。尝试移动的东西,但找不到解决方案。事情是不是在正确的地方,还是我错过了一些最终的代码?

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()

3 个答案:

答案 0 :(得分:1)

您正在检查密码和用户名是否为函数,显然它们不是。我相信您确实希望authenticateuser返回包含theusernamethepassword的字典。像这样:

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

(作为旁注,您应该使用_来分隔变量和函数名称中的单词)

答案 1 :(得分:1)

authenticateuser必须对输入参数执行某些操作,如果用户名/密码匹配则返回True,否则返回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

通常,当我们记录某人时,我们需要跟踪他们的登录状态。让我们为此创建一个简单的类(Session):

class Session:
    def __init__(self, username=None, loggedin=False):
        self.username = username
        self.loggedin = loggedin

登录功能现在可以询问用户名和密码,并致电authenticateuser查看它们是否正确。如果他们不正确,我们会增加wrongcount计数器。

在任何一种情况下,我们都会返回一个包含用户名以及用户是否已登录的会话:

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()

答案 2 :(得分:0)

您未正确调用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()

编辑:你的主要电话也没有做任何事情。 python代码逐行计算,因此你的“主”循环实际上就是你在“while loggedin == False”的地方。当你调用函数main时,你的程序基本完成了所有的事情,而main只是将用户名和密码设置为空字符串,但没有对这些值做任何进一步的处理