根据词典列表中的用户输入,打印特定的键值

时间:2018-07-24 11:32:11

标签: python list dictionary

我想从词典数据库列表中打印“帐户号”和“帐户余额”。首先,我已经从数据库验证了用户输入的名字,姓氏和生日,但是现在我不知道如何通过基于用户输入过滤列表中的字典来打印其他值。这是我的代码:

 import re
    import datetime
    from datetime import date
    from collections import defaultdict
    from operator import itemgetter
    userDetails=[]


    accountDetails = [
                        {"FirstName": "JOHN", "LastName": "DENIS","date of Birth": "01-06-1992", "Account Number": "432524352345234", "Account Balance": "50000"},
                        {"FirstName": "AKASH", "LastName": "MAHAJAN", "date of Birth": "04-02-1995","Account Number": "43252435242342", "Account Balance": "100000"},
                        {"FirstName": "AMAN", "LastName": "RANA","date of Birth": "11-04-1996", "Account Number": "432524324234424", "Account Balance": "60000"},]
                        ]

  firstName=str(input("Enter First Name").upper())

    def isDuplicate(value, fieldName, database):
                        for entry in database:

                          if fieldName in entry and entry[fieldName] == value:

                                return True
                        else:
                                return False
     while True:  

           if(isDuplicate(firstName, "FirstName", accountDetails))==True:

                            userDetails.append(firstName)
                            break

                      else:
                            print('Enter valid First Name')
                            firstName=str(input("Enter First Name").upper())


    lastName=str(input("Enter Last Name").upper())
    #LASTNAME=firstName.upper()
    isDuplicate(lastName, "LastName", accountDetails)
    while True:  

           if(isDuplicate(lastName, "LastName", accountDetails))==True:
                            print("entered correct lastname")
                            userDetails.append(lastName)
                            break


           else:
                            print('Enter valid last Name')
                            lastName=str(input("Enter last Name"))

    dateOfBirth=str(input("Enter date of birth"))
    while True:  

           if(isDuplicate(dateOfBirth, "date of Birth", accountDetails))==True:
                            print("entered correct date of birth")
                            userDetails.append(lastName)

                            break

                      else:
                            print('Enter valid date of birth')
                            dateOfBirth=str(input("Enter date of birth"))
    def printRequiredUserInfo(FirstName,AccountNumer,Accountbalance,Database):
            for entry in Database:

                if entry.get("FirstName")=FirstName:

                      print("Your Account number is "+ entry[AccountNumer])
                      print("Your Account balance is "+ entry[Accountbalance])
    printRequiredUserInfo(firstName,"Account Number","Account Balance",accountDetails)

1 个答案:

答案 0 :(得分:0)

for x in accountDetails:
    if (x["FirstName"]==firstName) and (x["LastName"]==lastName) and (x["date of Birth"]==dateOfBirth):
        print(x["Account Number"]," ",x["Account Balance"])