Python:创建登录系统 [英] Python: Create Login System

查看:104
本文介绍了Python:创建登录系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用python创建一个登录系统.

I would like to create a login system using python.

这是我正在做的一个小项目,这样我就可以掌握python并最终将其发送到我正在创建的webpage中.

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.

截至目前,我有这个:

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("")

这使您可以创建用户名和密码,并将其存储在列表中. 我在该if语句中检查了列表,以查看该用户是否已经存在(但我认为不可行)

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.

感谢一堆!

推荐答案

让我们一步一步走:

首先,检查该用户是否存在将总是告诉你这样做,因为你要添加的用户,以及检查,如果它的存在.因此,您编写的if语句可以,但是将其放在store_user.append:

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)

第二,您为while 1 == 1:编写的条件没有用(但仍然正确),您可以执行while 1(因为1的值为True),甚至可以做得更好while True:.

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.

第三,用户名/密码检查仅检查最后一个用户和密码.如果您使用userguess in store_user并使用相同的密码,则不会获得预期的结果.因此,您需要使用字典而不是列表.有了这些,您可以将分配给.在您的情况下,用户要输入密码,因此您将拥有:

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 :)

这篇关于Python:创建登录系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆