0 python dictionary python-2.7
我正在从名为"学习python艰难的方式"的书中学习python,我在exercise40中遇到了这段代码
cities = {'CA': 'San Francisco', 'MI': 'Detroit', 'FL': 'Jacksonville'}
cities['NY'] = 'New York'
cities['OR'] = 'Portland'
def find_city(themap, state):
    if state in themap:
        return themap[state]
    else:
        return "Not found."
# ok pay attention!
cities['_find'] = find_city
while True:
    print "State? (ENTER to quit)",
    state = raw_input("> ")
    if not state: break
# this line is the most important ever! study!
city_found = cities['_find'](cities, state)
print city_found
1.是否有必要在字典中添加"find_city"功能?
2.我可以删除函数"find_city"中的"themap"参数吗?
这是我的新代码,它仍然可以运行,没有任何错误:
cities = {"CA" : "San Francisco", "MI" : "Detroit", "FL" : "Jacksonville"}
cities["NY"]= "New York"
cities["OR"] = "Portland"
def find_city(state):
    if state in cities:
        return cities[state]
    else:
        return "Not Found."
while True:
    print "State? (ENTER to quit)",
    state = raw_input(">")
    if not state:
        break
    city_found = find_city(state)
    print city_found