Python: Create Login System
Solution 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:whilenot (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:
defadd_user(store):
user = raw_input('Create Username: ')
password = raw_input('Create Password: ')
if user in store:
print"That user already exsist"returnFalseelse:
store[user] = password
returnTrue# and call this 10 times for example...
global_store = dict()
for i inrange(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 :)
Solution 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.
Solution 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 inlist:
ifinput == item:
# item is in listbreak# 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:
defcheck_user(user, user_list):
for item inlist:
if user == item:
returnTruebreakreturnFalse
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.
Post a Comment for "Python: Create Login System"