2017-03-11 51 views
-2

当我输入错误的用户名和密码时程序正常工作,但我输入了正确的凭证后,它仍然返回,我创建了一个使用名为“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() 
+1

'authenticateuser(..)函数做,这个语句做什么:'如果密码== authenticateuser和用户名== authenticateuser:' – thebjorn

+0

从我的新手的理解,如果代码运行时没有函数,它会看起来像'如果密码== thepassword和用户名== theusername'。但是因为我创建了'authenticateuser'函数,我将它们替换为调用该函数。不知道是否清除它? –

+0

你在哪里调用这个函数?函数返回什么? – thebjorn

回答

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,看看他们是正确的。如果它们不正确,我们会增加错误的计数器。

在这两种情况下,我们返回一个包含用户名会话和用户是否登录:

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) 

现在主要可以调用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() 
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 

(作为一个侧面说明,你应该使用_在变量和函数名分隔单词)

+0

我还没有被教导分离这样的变量,是否有一个特定的原因,或者是阅读代码更容易的逻辑? –

+0

这只是Python惯例,以便其他程序员可以更好地阅读您的代码,并且可以更好地阅读其他程序员的代码。 – Unlocked

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代码逐行评估,所以你的“main”循环实际上就是你在“while loggedin == False”时做的地方。通过调用函数main的时候,你的程序已基本完成了一切,主只设置为空字符串的用户名和密码,但进一步不做任何处理这些值