Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

IN PYTHON: This function will take as input a list and a tuple. The list will be

ID: 3677230 • Letter: I

Question

IN PYTHON:

This function will take as input a list and a tuple. The list will be a list of lists – each element will consist of a list containing a first element which is a list with two strings – the first string indicating a city and the second indicating a state – and a second element which is an integer indicating the population for that state:
[[[city,state],population], [[city,state],population] …]
The second parameter is a tuple with a city, state values.
The function will return a string with the city name given in the tuple and its associated population.
You are required to use a dictionary and a try/except with the except on “KeyError” specifically.
If the city, state tuple given does not correspond to a (city, state) entry in the given clist, the following error string is returned:
“No such city.”

SAMPLE RUNS
cityDict([[["Indianapolis","IN"],820445],[["Joliet","IL"],147433],[["Fort Wayne", "IN"],253691],[["Cedar Rapids","IO"],12429],[["Chicago","IL"],2695598],[["Evansville","IN"],117429],[["South Bend","IN"],1011681],[["Des Moines","IO"],207510],[["Davenport","IO"],102157],[["Sioux City","IO"],82459],[["Rockford","IL"],152871],[["Aurora","IL"],197899]],("Chicago","IL”))
will return the following in this exact format:
('Chicago', 2695598)

Explanation / Answer

# first we define cityDict function here
# in this function first we get city and state value
# cu_city = tup1[1][0]
# cu_state = tup1[1][1]
# in cu_city varible we add city and in cu_state we add state value
# we define function golable varible x we check in end of function if x == 0 mean result not found and we return message
# we run for loop according to over requrement
# in if found == cu_city: we check city match or not
# in if cu_state == city[1]: we check state value match and not
# if both condation true then we return city and population

def cityDict(tup1):
cu_city = tup1[1][0]
cu_state = tup1[1][1]
x = 0
for fruit in tup1: # Second Example
for popl in fruit: # Second Example
for city in popl:
if type(city) is list:
for found in city:
if found == cu_city:
if cu_state == city[1]:
x=1
w = (found,popl[1])
return w
if x == 0:
return 'No such city.'
  


# first we create list according to your requrement in list we pass city and state value ("Chicago","IL")
# we call cityDict function here and pass over value here
# create sample code and i check. if i change value in ("Chicago","IL") to ("Chicagooo","IL"), ("Chicago","ILE") then return No such city. message
# you also change according to you r requrement
tup1 = ([[["Chicago","IL"],2695598],[["Evansville","IN"],117429],[["South Bend","IN"],1011681]],("Chicago","IL"));
result = cityDict(tup1)
print result