【问题标题】:Coping with simple database with username and password使用用户名和密码处理简单的数据库
【发布时间】:2016-03-10 08:18:29
【问题描述】:

我是 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.")

谁能帮帮我?

【问题讨论】:

    标签: python-3.x


    【解决方案1】:

    您不需要为此使用模块,它只会增加复杂性,在这种情况下没有任何好处。只需按照一般约定在文件顶部定义您的函数(在所有导入下方,在这种情况下没有)。然后,您在 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()
    

    其他几个cmets:

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

    【讨论】:

      猜你喜欢
      • 2018-02-13
      • 2016-02-05
      • 2014-06-27
      • 1970-01-01
      • 2016-12-13
      • 2016-12-04
      • 1970-01-01
      • 2016-01-31
      • 1970-01-01
      相关资源
      最近更新 更多