Search code examples
pythonlistdictionarydefaultdict

Verify the date of birth taken as user input with the value stored in nested dictionary in a list in python


I am trying to verify date of birth taken from user and comparing it with the value stored in dictionary database. But it is not verifying the value even if I have entered the right value of date of birth. Here is my code:

from collections import defaultdict



accountDetails = [
    {"FirtsName": "JOHN", "LastName": "DENIS","date of Birth": "01-06-1992", "Account Number": "432524352345234", "Account Balance": "50000"},
    {"FirtsName": "AKASH", "LastName": "MAHAJAN", "date of Birth": "04-02-1995","Account Number": "432524352345234", "Account Balance": "50000"},
    {"FirtsName": "AMAN", "LastName": "RANA","date of Birth": "11-04-1996", "Account Number": "432524352345234", "Account Balance": "50000"},
    {"FirtsName": "ANKUR", "LastName": "JAIN","date of Birth": "21-05-1990", "Account Number": "432524352345234", "Account Balance": "50000"},
]


d = defaultdict(lambda: defaultdict(dict))
for item in accountDetails:
    d[item['FirtsName']][item['LastName']][item['date of Birth']] = item

# get valid first name
while True:
    first_name_input = input('Enter First Name:\n').upper()
    if first_name_input in d:
        break
    else:
        print('Enter valid First Name')

# get valid last name
while True:
    last_name_input = input('Enter Last Name:\n').upper()
    if last_name_input in d[first_name_input]:
        break
    else:
        print('Enter valid Last Name')
                         # get valid last name
while True:
    dob_input = input('Enter dob:\n')
    if  dob_input in d[first_name_input]:
        break
    else:
        print('Enter valid dob')


print(d[first_name_input][last_name_input])

The user has put the value as-04-02-1995


Solution

  • You need to test versus keys in the second level of your nested dictionary:

    while True:
        dob_input = input('Enter dob:\n')
        if dob_input in d[first_name_input][last_name_input]:
            break
        else:
            print('Enter valid dob')
    

    This becomes clear when you print the structure of the nested dictionary you have created:

    print(d)
    
    defaultdict(<function __main__.<lambda>>,
                {'AKASH': defaultdict(dict,
                             {'MAHAJAN': {'04-02-1995': {'Account Balance': '50000',
                                'Account Number': '432524352345234',
                                'FirtsName': 'AKASH',
                                'LastName': 'MAHAJAN',
                                'date of Birth': '04-02-1995'}}}),
                 ...
                 'JOHN': defaultdict(dict,
                             {'DENIS': {'01-06-1992': {'Account Balance': '50000',
                                'Account Number': '432524352345234',
                                'FirtsName': 'JOHN',
                                'LastName': 'DENIS',
                                'date of Birth': '01-06-1992'}}})})