美文网首页
字典应用

字典应用

作者: huojusan | 来源:发表于2018-05-24 10:48 被阅读15次
    # -*- coding: utf-8 -*-
    # @Author: yt
    # @Date:   2018-05-24 10:34:05
    # @Last Modified by:   yt
    # @Last Modified time: 2018-05-24 10:45:54
    
    
    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."
    
    cities['_find'] = find_city
    
    while True:
        print "State?(ENTER to quit)",
        state = raw_input(">>>")
    
        if not state:break
    
        city_found = cities['_find'](cities,state)
        print city_found
        
    
    运行结果: 图1

    相关文章

      网友评论

          本文标题:字典应用

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