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!
By the end of this guided project, you'll be able to create a login system using python's popular library Tkinter. You'll learn how to create a Graphical User Interface (GUI) in Python from the scratch. You'll learn how to create windows, labels, entry boxes and buttons.
The Python script is as follows: print "Login Script" import getpass CorrectUsername = "Test" CorrectPassword = "TestPW" loop = 'true' while (loop == 'true'): username = raw_input("Please enter your username: ") if (username == CorrectUsername): loop1 = 'true' while (loop1 == 'true'): password = getpass.
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 :)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With