Python 中的简单用户名和密码应用程序 [英] Simple username and password application in Python

查看:58
本文介绍了Python 中的简单用户名和密码应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用字典构建一个简单的登录名和密码应用程序.除了检查登录名是否与密码匹配的部分(在底部显示登录成功!")之外,它工作正常.

I'm trying to build a simple login and password application using a dictionary. It works fine except the part where it checks if the login matches the password (in the bottom where it says "Login successful!").

如果我创建登录名 'a' 和密码 'b',然后创建登录名 'b' 和密码 'a',如果我尝试使用登录名 'a' 和密码登录'一种'.它只是检查这些字符是否存在于字典中的某处,而不是它们是否是一对.

If I were to create login 'a' and password 'b', and then create login 'b' and password 'a', it would log me in if I tried to log in with login 'a' and password 'a'. It just checks if those characters exist somewhere in the dictionary, but not if they are a pair.

有什么建议可以解决这个问题吗?

Any suggestions how to fix this?

users = {}
status = ""

while status != "q":
    status = raw_input("Are you a registered user? y/n? Press q to quit: ")  

    if status == "n": #create new login
         createLogin = raw_input("Create login name: ")

         if createLogin in users: # check if login name exist in the dictionary
             print "Login name already exist!\n"
         else:
             createPassw = raw_input("Create password: ")
             users[createLogin] = createPassw # add login and password
             print("\nUser created!\n")     

    elif status == "y": #login the user
        login = raw_input("Enter login name: ")

        if login in users:
           passw = raw_input("Enter password: ")
           print

           if login in users and passw in users: # login matches password
               print "Login successful!\n"

        else:
            print
            print("User doesn't exist!\n")

编辑

既然这已经起作用了,为了便于阅读,我正在尝试将应用程序划分为三个功能.它有效,只是我得到无限循环.

Now that this is working, I'm trying to divide the application to three functions, for readability purposes. It works, except that I get infinite loop.

有什么建议吗?

users = {}
status = ""

def displayMenu():
    status = raw_input("Are you a registered user? y/n? Press q to quit: ")  
    if status == "y":
        oldUser()
    elif status == "n":
        newUser()

def newUser():
    createLogin = raw_input("Create login name: ")

    if createLogin in users: # check if login name exists
        print "\nLogin name already exist!\n"
    else:
        createPassw = raw_input("Create password: ")
        users[createLogin] = createPassw # add login and password
        print("\nUser created!\n")     

def oldUser():
    login = raw_input("Enter login name: ")
    passw = raw_input("Enter password: ")

    # check if user exists and login matches password
    if login in users and users[login] == passw: 
        print "\nLogin successful!\n"
    else:
        print "\nUser doesn't exist or wrong password!\n"

while status != "q":            
    displayMenu()

推荐答案

现在您正在检查给定的密码 passw 是否与 中的任何 keys 匹配用户(不对).您需要查看输入的密码是否与该特定用户的密码匹配.由于您已经检查过用户名是否存在于字典的键中,因此您不必再次检查,因此请尝试以下操作:

Right now you are checking if the given password, passw, matches any keys in users (not right). You need to see if the password entered matches that particular user's password. Since you have already checked if the username exists in the dictionary's keys you don't have to check again, so try something like:

if passw == users[login]:
    print "Login successful!\n"

对于您更新的代码,我将通过无限循环"假设您的意思是您不能使用 q 退出程序.这是因为当您在 displayMenu 中时,您将用户输入保存在名为 status局部变量中.此局部变量引用您正在检查的相同status

For your updated code, I'm going to assume by "infinite loop" you mean that you cannot use q to exit the program. It's because when you're inside displayMenu, you save user input in a local variable named status. This local variable does not refer to the same status where you are checking,

while status != "q": 

换句话说,您在两个不同的作用域中使用变量status(改变内部作用域不会改变外部作用域).

In other words, you are using the variable status in two different scopes (changing the inner scope does not change the outer).

有很多方法可以解决这个问题,其中一种方法是改变,

There are many ways to fix this, one of which would be changing,

while status != "q":
    status = displayMenu()

并在displayMenu的末尾添加一个return语句,就像这样,

And adding a return statement at the end of displayMenu like so,

return status

通过这样做,您将 status 的新值从 displayMenu 的局部范围保存到脚本的全局范围,以便 while> 循环可以正常工作.

By doing this, you are saving the new value of status from local scope of displayMenu to global scope of your script so that the while loop can work properly.

另一种方法是将这一行添加到displayMenu的开头,

Another way would be to add this line to the beginning of displayMenu,

global status

这告诉 Python displayMenu 中的 status 指的是全局范围的 status 变量,而不是新的局部范围变量.

This tells Python that status within displayMenu refers to the global scoped status variable and not a new local scoped one.

这篇关于Python 中的简单用户名和密码应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆