如何在Python中向数据库列表添加新的用户名和密码?

时间:2013-05-23 22:22:56

标签: python database login add username

我是Python新手,我在下面编写了这段代码。它只是一个简单的登录代码,允许您注册或登录。

现在我可以登录了。如您所见,我将用户名和密码放在变量database

在函数register()中,我尝试以(用户名,密码)的形式将newusernamenewpassword添加到database列表,以便{{1看到它的功能。

login()

可悲的是,import time import sys import getpass database = [ ("Test1", "123"), ("Test2", "000") ] def login(): time.sleep(1) print("Welcome. Please login.") while True: time.sleep(1) username = input("Username: ") password = getpass.getpass("Password: ") time.sleep(1) if (username, password) in database: print("Welcome, " + username) main() else: print("User not found. Try again.") def logout(): time.sleep(1) print("Logout?") lgout = input(">>") if lgout == ("yes") or lgout == ("Yes") or lgout == ("YES"): time.sleep(1) print("Logout successful") main2() elif lgout == ("no") or lgout == ("No") or lgout == ("NO"): print("Logout unsuccessful") main() else: print("Command not valid") def main(): time.sleep(1) print("Current commands: Logout") while True: command = input(">>") if command == ("Logout"): logout() else: print("Command not valid") def main2(): time.sleep(1) print("Hello, would you like to login, register or exit?") while True: command2 = input(">>") if command2 == ("Login") or command2 == ("login") or command2 == ("LOGIN"): login() elif command2 == ("Register") or command2 == ("register") or command2 == ("REGISTER"): register() elif command2 == ("Exit") or command2 == ("exit") or command2 == ("EXIT"): sys.exit() else: print("Command not valid") def register(): print("Register your information below") newusername = input("Username: ") newpassword = getpass.getpass("Password: ") print("Success! Please login!") login() main2() 不起作用,因为它不是字典。

1 个答案:

答案 0 :(得分:1)

这里的“数据库”是一个元组列表。

要将某些内容附加到列表的末尾,请使用.append()。要构造元组,请使用包含以逗号分隔的值的括号。将这两者放在一起:

database.append( (newusername, newpassword) )