I have written the following code for getting an output of the various districts located in the given city and their respective postal codes. I want my code to be able to receive input from the user (District Name) and output the City in which it is located and it's postal code. If the user inputs a postal code they'll get the respective district(s).
zipcode = {"Trap City":{"C District": 100, "D District": 103, "E District": 104, "S District": 105}, "Zap City":{"R District": 200, "D District": 201},"Los City": {"X District": 207, "Y District": 208}}district=input('Enter your district: ')for city in zipcode:if district in city:print(city,zipcode[city][district])d_district = {k2: (k1, v2) for k1, v1 in zipcode.items() for k2, v2 in
v1.items()}print(d_district){'C District': ('Trap City', 100),
'D District': ('Zap City', 201),
'E District': ('Trap City', 104),
'R District': ('Zap City', 200),
'S District': ('Trap City', 105),
'X District': ('Los City', 207),
'Y District': ('Los City', 208)}d_code = {v2: k2 for k1, v1 in zipcode.items() for k2, v2 in
v1.items()}print(d_code){100: 'C District',
103: 'D District',
104: 'E District',
105: 'S District',
200: 'R District',
201: 'D District',
207: 'X District',
208: 'Y District'}
Output:
Enter your district: X District
{'C District': ('Trap City', 100), 'D District': ('Zap City', 201), 'E
District': ('Trap City', 104), 'S District': ('Trap City', 105), 'R
District': ('Zap City', 200), 'X District': ('Los City', 207), 'Y
District': ('Los City', 208)}
{100: 'C District', 103: 'D District', 104: 'E District', 105: 'S
District', 200: 'R District', 201: 'D District', 207: 'X District',
208: 'Y District'}
>>>
The program instead just dumps all the information instead of searching for the value?