问题
I would like to create a login system using python.
This is a small project that I am doing so that I could get a grip on python and eventually send this over to a webpage that I am creating.
As of right now I have this:
user = raw_input('Create Username: ')
password = raw_input('Create Password: ')
store_user =[roger, Jon]
store_pass =[tennis, soccer]
store_user.append(user)
store_pass.append(password)
if user in store_user:
print "That user already exsist"
while 1 == 1:
userguess=""
passwordguess=""
key=""
while (userguess != user) or (passwordguess != password):
userguess = raw_input('User Name: ')
passwordguess = raw_input('Password:')
print "Welcome,",user, ". Type Lock to Lock or Type Create to create another user."
print store_user
print store_pass
while key != "lock":
key = raw_input("")
This gets you to create a user name and password and would store them in a list. I threw in that if statement to check the list to see if there the user already exist ( but i don't think that works)
I just want a little bit of guidance on how to get the whole thing to loop: for example you choose if you have a login or not, after you login you have the option to create another, and it would check to see if that user exists already.
Thanks a bunch!
回答1:
Let us go step by step:
First, the check if the user exists will always tell you it does, because you are adding the user, then checking if it exists. So the if statement you wrote is OK, but put it before the store_user.append:
if user in store_user:
print "That user already exsist"
else:
store_user.append(user)
store_pass.append(password)
Second, the condition you wrote for while 1 == 1: is useless (but still right), you could do while 1 (because 1 evaluates to True) or even better while True: simply.
Third, the username/password check only checks for the last user and password. If you used userguess in store_user and the same for password, you will not get the expected result. So, you need to use dictionaries instead of lists. With these, you can assign a key to a value. In your case a user to a password, so you will have:
store = dict()
# the code to get the user/password
store[user] = password # you "save" the value password for the key user
# then to check:
while not (userguess in store and store[userguess] == passwordguess):
# try again
Read more about dictionaries.
Then, you could store each "logical" procedure in a function to make your code more readable and re-usable. For example:
def add_user(store):
user = raw_input('Create Username: ')
password = raw_input('Create Password: ')
if user in store:
print "That user already exsist"
return False
else:
store[user] = password
return True
# and call this 10 times for example...
global_store = dict()
for i in range(10):
add_user(global_store)
(Not to mention using classes, etc.) Do the same for the check password part, and then you can use the loop you made with a bit of changes to do what you expect...
I hope I was clear, and that I helped :)
回答2:
This is probably not the answer your were expecting, but here it goes: DON'T re-invent the wheel.
Use existing tools as much as possible and focus on the functionality you can't get for free.
回答3:
this is a good start. I would recommend you checking out: http://www.greenteapress.com/thinkpython/, using data structures like dictionaries will greatly simplify this task.
To see if an item is already in a list, you could iterate through it;
for item in list:
if input == item:
# item is in list
break
# item is not in list
A strategy that may also help is to modularize your code into functions. For example, you could have a check_name function that returns True if a user is registered:
def check_user(user, user_list):
for item in list:
if user == item:
return True
break
return False
There are also shorter ways to do this, such as: Python: check if value is in a list no matter the CaSE
When you do eventually serve your project online, look into a framework like Django or Flask, they greatly simplify things like logins, managing users, etc. Good luck.
来源:https://stackoverflow.com/questions/10304712/python-create-login-system