使用用户名和密码处理简单数据库

时间:2016-03-09 17:53:30

标签: python-3.x

我是学习者以及Python 3.5.1的新用户 我正在尝试制定一个简单的反馈程序,只是创建存储在列表中的帐户以及登录它们。如果用户已成功登录或密码错误,我应该向用户提供反馈。我打算使用模块功能,但我想保持简单,尽可能避免使用它。以下是我所做的代码,它不起作用。

user={}
import lib`

while 1:
    print("Welcome to the login portal.")
    print("Do you have an existing account? y/n")
    answer=input()
    if answer==("y").lower():
        login()
    if answer==("n").lower():
        create()
    if answer==("q").lower():
        quit()
    else:
        tryagain()

**在lib文件中,代码为:*

def tryagain():
    while 2:
        print("Please try again or press q to quit.") 
        print("Do you have an existing account? y/n")

def create():
    print("Please create a username and password.")
    print("Username: ")
    createun=input()
    print("Password: ")
    createps=input()
    print("Account creation successful. Please login to continue.")
    login()

def login():
    print("Please enter your username: ")
    loginun=input()
    if createun==loginun:
        print("Please enter your password: ")
        loginps=input()
        if createps==loginps:
            print("Account validation successful.")
        elif createps!=loginps:
            print("Password incorrect. Please try again.")
    else:
        print("Login Failed!")
if __name__== "__main__":
    print("Module successfully loaded.")
    input("Press the enter key to exit.")

有人能帮助我吗?

1 个答案:

答案 0 :(得分:0)

您不需要为此使用模块,它只会增加复杂性,在这种情况下没有任何好处。根据一般惯例,只需在文件顶部定义您的函数(在所有导入下面,在本例中为none)。然后,您可以在main()函数中调用函数。这是一个这样做的例子。这应该让你开始寻找你想要的东西。

def account_create(userNamePassDict):
    print('Please create a username and password.')
    userName = input('Username: ')
    userPassword = input('Password: ')
    userNamePassDict[userName] = userPassword
    print('Account creation successful')
    return userNamePassDict


def account_login(userNamePassDict):
    print('Please log in using your user name and password')

    userName = input('Username: ')
    while userName not in userNamePassDict:
        print('User name is not recognized. Please try again.')
        userName = input('Username: ')

    userPassword = input('Password: ')
    while userNamePassDict[userName] != userPassword:
        print('Password is incorrect. Please try again.')
        userPassword = input('Password: ')

    print('Account validation successful.')


def main():
    userNamePassDict = {}
    print('Welcome to the login portal.')
    while len(userNamePassDict) != 5: # Keep looping till 5 users have been created
        answer = input('Do you have an existing account? y/n: ')
        if answer==('y').lower():
            account_login(userNamePassDict)
        if answer==('n').lower():
            userNamePassDict = account_create(userNamePassDict)
        if answer==('q').lower():
            break

    print('Here is the User Database:')
    print(userNamePassDict)


if __name__ == '__main__':
    main()

其他几条评论:

  • 我不确定while 1while 2的含义。如果你想创建无限循环,while True是一种方法,虽然通常有更好的方法。
  • input()将字符串作为提示用户的参数。您可以为自己保存一份单独的打印声明。
  • 编写超出几行代码的内容时,不要编写所有代码然后进行测试。随着时间的推移,编写和测试它会容易得多。在进行更改时经常进行测试。