IN PYTHON: This function will take as input a list and a tuple. The list will be
ID: 3678075 • 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
d = {}
for m in range(5):
user = input('Enter city,state with population')
# use raw_input(prompt) if using Python
data = user.split()
d[data[0]] = int(data[1])