如何在dict列表中检查密码?

2024-03-28 11:20:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我刚开始使用PyCharm,我想做一个程序,将检查用户名和帐户,但我是初学者,我不知道我应该做什么。你知道吗

bank = {"Akim": [1234, 98], "Argo": [7432, 87], "Anton": [1236, 70], "HaCK": [10101, 0]}
'''Here I have my list with names(keys) and password(first value) and amount of money on their account(second value)'''

#I want to have 2 ways of solving this problem
def open_2 ():
    name1=input("name")
    passw=input("pass")
    #I want to make Python to check is there account with specific username and password.
    if ((name1)[passw]) in bank:
        print("you passed")
    else:
        print("mo")
        #But it gives me error.



def open():
    name = input("PLease write your name")
    p= input ("Please write your password")
    op = (bank.get(name))
    if op != None:
        # I want to make check point here but it doesn't work at all
        q= (bank.get(name[p]))
        #Here is the main problem but I have no idea what is the problem
        if q == (bank.get(name[0])):
            print ("You successfully logged on")
    else:
        print("Password or name is incorrect.Please try again, or if you don't have account you can register now for free.")
        n= input("Please type yes, if you want to, or no if you don't want to:")
        if n=="yes":
            register()
    return ()

Tags: andtonameyouinputifishave
1条回答
网友
1楼 · 发布于 2024-03-28 11:20:41

我建议您更改open()函数的名称,因为它也是内置python函数的名称。你知道吗

我做了一些细微的改动,添加了一些评论,但你可以研究一下其中的区别。不管怎样,这段代码对我来说有点用:

bank = {"Akim": [1234, 98], "Argo": [7432, 87], "Anton": [1236, 70], "HaCK": [10101, 0]}

def open_2 ():
    name1=input("name")
    passw=input("pass")
    #I want to make Python to check is there account with specific username and password.
    try:
        if (bank[name1][0]==passw): # I changed passw to 0 since that is the index for the passw
            print("you passed")

        else:
            print("mo")
        #But it gives me error. (Since you had passw as an index and didn't use bank[name]
    except KeyError:
        print name1,' is not a valid username!'




def open_1():
    name = input("PLease write your name")
    p= input ("Please write your password")
    op = bank.get(name)
    if op != None:
        # This works for me now.
        # q= (bank.get(name)[0]) Don't see why you would need this line
        #Here is the main problem but I have no idea what is the problem
        '''The problem is that (bank.get(name[p])) uses p as an index for name, but p isn't a valid Index for name, and you don't even need any index from name'''                       
        if p == (bank.get(name)[0]):
            print ("You successfully logged on")
        else:
            print("Password is incorrect.Please try again, or if you don't have account you can      register now for free.")
            n= input("Please type yes, if you want to, or no if you don't want to:")
            if n=="yes":
                register()
    else:
        print 'Name is not registred'
    return ()

相关问题 更多 >