Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python用户注册_Python_Python 3.x_Login_Registration - Fatal编程技术网

Python用户注册

Python用户注册,python,python-3.x,login,registration,Python,Python 3.x,Login,Registration,我正在尝试制定一个有效的用户注册计划。当询问用户名时,系统将查看输入的文本是否已存储。如果用户名已经存储,它将询问该用户的密码。但是,如果用户名尚未存储,它将要求输入密码。输入这些内容后,脚本将附加到.py文件的.txt中以创建新帐户。创建帐户后,脚本可以读取.txt或.py文件中的登录信息。我当前的登录代码: loop = "true" while(loop == "true"): username = input("Enter Username: ") password =

我正在尝试制定一个有效的用户注册计划。当询问用户名时,系统将查看输入的文本是否已存储。如果用户名已经存储,它将询问该用户的密码。但是,如果用户名尚未存储,它将要求输入密码。输入这些内容后,脚本将附加到.py文件的.txt中以创建新帐户。创建帐户后,脚本可以读取.txt或.py文件中的登录信息。我当前的登录代码:

loop = "true"
while(loop == "true"):
    username = input("Enter Username: ")
    password = input("Enter Password: ")
    h = input ("Do You Need Help [Y/N]: ")
    if(h == "Y" or h == "y" or h == "yes" or h == "Yes"):
    print ("Enter username and password to login. If you do not have an account yet, enter 'Guest' as the username and press enter when it asks for the password.")
    elif(h == "N" or h == "n" or h == "no" or h == "No"):
        print (" >> ")
    if(username == "Hello World" and password == "Hello World" or username == "Test User" and password == "Test User" or username == "Guest"):
        print ("Logged in Successfully as " + username)
        if(username == "Guest"):
            print ("Account Status: Online | Guest User")
        if not (username == "Guest"):
            print ("Account Status: Online | Standard User")

如何创建一个python可以读取用户名和密码的数据库?另外,如何使python能够在数据库中添加更多的用户名和密码

这是Python v3.3.0 Mac OSX 10.8


提前谢谢你

尝试使用
pickle
模块:

>>> import pickle
>>> myusername = "Hello"
>>> mypassword = "World"
>>> login = [myusername, mypassword]
>>> pickle.dump(login, open("%s.p" % login[0], "wb")) #Saves credentials in Hello.p, because Hello is the username
>>> #Exit
现在把它拿回来

>>> import pickle
>>> try:
...    password = pickle.load(open("Hello.p", "rb"))[1]
...    username = pickle.load(open("Hello.p", "rb"))[0]
... except IndexError: #Sees if the password is not there
...    print("There is no information for those credentials")
...
>>> password
'mypassword'
>>> username
'myusername'
如果没有密码或用户名,它将打印
没有这些凭据的信息
。。。希望这有帮助


还有一个提示:如果(h=='n'…,不要费心看
,只要做一个
h.lower().startswith(“n”)==True
。lower()
使所有内容都小写,
str.startswith(“n”)
检查
str
是否以字母n开头。

您遇到了什么问题?如何创建一个python可以从中读取用户名和密码的数据库?此外,如何使python可以添加更多用户名和密码?例如
导入sqlite3
,当然!这就是有一点帮助。我是否为第一部分和第二部分制作不同的.py文档?感谢基本上您可以做的是向用户询问用户名和密码,如果用户名和密码不存在(会引发索引器),询问用户是否要继续作为来宾或要创建凭据。如果是创建,请使用
input()
获取用户名,并查找有关
getpass.getpass()
获取密码的详细信息。然后可以使用
pickle.dump()
,如第一节所示,转储信息。因此,基本上,是的,您使用的是同一个文件。如果您想知道如何完成此操作的示例,请在我个人资料的电子邮件中向我发送电子邮件,我将向您发送代码!祝您好运!不是pickle不太好,而是SQLite可能更适合您的目标。