简体   繁体   English

如何将用户输入添加到列表中并能够看到它?

[英]How do I add user input to a list and able to see it?

I am trying to make a sign up system in python 3.7 but I don't know how to permanently add a users login to a list and not having to sign up for the same account every time you open the program. 我正在尝试在python 3.7中创建一个注册系统,但我不知道如何将用户登录名永久添加到列表中,而不必每次打开该程序都注册同一帐户。

I was not able to make up a solution to this problem. 我无法解决此问题。

Usernames = ['User1', 'User2']
Passwords = ['password123', 'password123']


print('Type SU to Sign Up or type LI to Log In!')
UOption = input('Option>> ')

if UOption == 'SU':
    SI = True
    LI = False
    if SI == True:
        print('Signing Up!')
        SUUsername = input('Username>> ')
        SUEmail = input('Email>> ')
        SUPassword = input('Password>> ')

    Usernames.append(SUUsername)
    Emails.append(SUEmail)
    Passwords.append(SUPassword)
    LI = True
    SI = False

I am expecting when I get this working that the user will be able to sign up once then be able to log in if they reopen the program without having to sign up again. 我希望当我开始工作时,如果用户重新打开程序而不必再次注册,则用户将能够注册一次,然后能够登录。

You could use the pickle module: 您可以使用pickle模块:

Firstly, to create the necessary files, run the following code in the folder that your program is saved in: 首先,要创建必要的文件,请在将程序保存到的文件夹中运行以下代码:

import pickle
pickle.dump([],open("usernames.dat","wb"))
pickle.dump([],open("emails.dat","wb"))
pickle.dump([],open("passwords.dat","wb"))

In your program, add: 在程序中,添加:

import pickle

at the start of the program 在程序开始时

Replace the lines: 替换行:

Usernames = ['User1', 'User2']
Emails = ['Email1', 'Email2'] # I'm assuming this line has just been missed off your question
Passwords = ['password123', 'password123']

With: 带有:

Usernames = pickle.load(open("usernames.dat","rb"))
Emails = pickle.load(open("emails.dat","rb"))
Passwords = pickle.load(open("passwords.dat","rb"))

To read from the files 从文件中读取

And finally add these lines at the end of your program to update the files with the new user's details 最后,在程序末尾添加这些行,以使用新用户的详细信息更新文件

pickle.dump(Usernames,open("usernames.dat","wb"))
pickle.dump(Emails,open("emails.dat","wb"))
pickle.dump(Passwords,open("passwords.dat","wb"))

This is a link to more details on how to use pickles, if your interested 如果您有兴趣,这是一个有关如何使用泡菜的更多详细信息的链接。

https://www.tutorialspoint.com/python-pickling https://www.tutorialspoint.com/python-pickling

For source code including these edits see here: 有关包括这些编辑的源代码,请参见此处:

https://pastebin.com/H4ryP6cT https://pastebin.com/H4ryP6cT

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM