用python实现简单的创建用户并登陆(python2.6版本)

在python2.6版本上,简单实现创建用户以及登陆功能,源代码如下:

#login system

class UserInfo:
    '''
    *************************************
    * Welcome to Tom and Selina system  *
    *************************************
    '''
    UserName = {'Tom':'1234','Selina':'1234'}
    def __init__(self):
        pass


    def Option(self):
        choice=raw_input("Please input your operation,L(ogin) or R(egister):")
        print "choice:",choice
        while choice not in ('L','l', 'R', 'r'):
            choice=raw_input("Your input is not correct, please input L(l) or R(r):")
        return choice




    def Login(self):
        username=raw_input("Please input your username:")
        password=raw_input("Please input your password:")
        if self.UserName.has_key(username) and self.UserName.__getitem__(username)==password:
            print "The right user information."
            print "You have login the system."
            return None
        elif self.UserName.has_key(username) and self.UserName.__getitem__(username)!=password:
            print "Error, your password is not right."
            self.Login()
        else:
            print "Error, your username is not exist."
            self.Login()


    def CreateUser(self):
        username=raw_input("Please input the new username:")
        while self.UserName.has_key(username):
            print "The username is exist already, please input another one."
            username=raw_input("Please input the new username:")


        password=raw_input("Please input your password:")
        self.UserName[username]=password
        print "UserName list :",self.UserName
        choice=raw_input("Do you want to L(ogin) or E(ixt):")
        if choice in ('L','l'):
            self.Login()
        elif choice not in ('E','e'):
            print "Unknown input. Exit directly."
            exit()
        else:
            print "Exit the system."
            exit()


u1=UserInfo()
print u1.__doc__
option1=u1.Option()
if option1 in ('L','l'):
    u1.Login()
else:
    u1.CreateUser()

你可能感兴趣的:(用python实现简单的创建用户并登陆(python2.6版本))