2014-11-02 2 views
-3

방금 ​​PyCharm을 사용하기 시작했으며 사용자 이름과 계정을 확인할 프로그램을 만들고 싶습니다. 그러나 초보자이며 무엇을해야할지 모르겠습니다.dict 목록에서 암호를 확인하는 방법은 무엇입니까?

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() 
+2

직면 한 오류는 무엇입니까? – ThePredator

+0

'(name1) [passw]'구조가''Akim ": [1234, 98]'과 다르며 또한 입력이 숫자인지 확인하십시오. – badc0re

답변

0

open() 함수의 이름은 내장 된 파이썬 함수의 이름이기 때문에 변경하는 것이 좋습니다.

약간의 변경을가했지만 몇 가지 의견을 추가했지만 차이점을 연구 할 수는 있습니다. 어쨌든이 코드는 나를 위해 다소 효과가 있습니다 :

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() 
관련 문제