In Python, how do I check if an instance of my cla

2019-08-02 01:56发布

I'm making a program that asks the user to login, and I'm wondering how I can make it so it checks if an instance of my Username class exists. Oh and please excuse my sloppy and disorganized coding, I'm not very good at it.

quit_login = 1
class Usernames:
    def __init__(self, password):
        self.password = password
testlogin = Usernames("foo")
def login_e():
    a = raw_input("Please enter a username: ")
    new_pass = ""
    if isinstance(a, Usernames):
        a = Usernames(new_pass)
        print Usernames
    else:
        login_pass = raw_input("What is your password?\n")
        if login_pass == a.password:
            print "Hello", a
        else:
            print "Incorrect password"
while quit_login != 0:
    login_e()

1条回答
甜甜的少女心
2楼-- · 2019-08-02 02:13

The missing piece is a collection to hold your Usernames instances. For this particular scenario, you probably want a dictionary.

>>> myDict = {}
>>> myDict['foo'] = 5
>>> 'foo' in myDict
True
>>> myDict['foo']
5
>>> myDict.get('bar', 'nope')
'nope'
>>> 
查看更多
登录 后发表回答