美文网首页
笨方法学python 习题40

笨方法学python 习题40

作者: d1b0f55d8efb | 来源:发表于2017-07-26 11:35 被阅读0次
    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 = input("> ")
    
        if not state: break
    
        # this line is the most important ever! Study!
        city_found = cities['_find'](cities, state)
        print (city_found)
    

    cities[‘_find’] = find_city这里的cities[‘_find’](是创建的字典中的一个元素)被赋值成find__city函数,也可以理解为cities[‘_find’]是一个函数变量。

    相关文章

      网友评论

          本文标题:笨方法学python 习题40

          本文链接:https://www.haomeiwen.com/subject/luunkxtx.html