zoukankan      html  css  js  c++  java
  • python 教程 第十四章、 地址薄作业

    第十四章、 地址薄作业

    #A Byte of Python
    #!/usr/bin/env python
    import cPickle
    import os 
    #define the contacts file, list
    global file_contacts
    global list_contacts
    file_contacts = 'AddressBook.txt'
    list_contacts = [] 
    #delete the file
    try:
        file_path = os.getcwd() + os.sep + file_contacts
        if os.path.isfile(file_path):
            os.remove(file_contacts)
            f = file(file_contacts,'w')
            f.close() 
         #define the class of contacts and implement the methods
        class class_contacts:
            def __init__(self, list_contacts):
                self.contacts = list_contacts
            def append(self, name, mail):
                dict_contacts = {'Name': name, 'Mail': mail}
                self.contacts.append(dict_contacts)
            def find(self, name):
                found = False
                for instance_contacts in list_contacts:
                    if name == instance_contacts['Name']:
                        found = True
                        print " Found:", name, ", PASS"
                        break
                if not found:
                    print " Found:", name, ", FAIL" 
            def modify(self, name, mail):
                for instance_contacts in list_contacts:
                    if name == instance_contacts['Name']:
                        instance_contacts['Mail'] = mail
                        break
            def delete(self, name):
                index = -1
                for instance_contacts in list_contacts:
                    index += 1
                    if name == instance_contacts['Name']:
                        del list_contacts[index]
            def show(self):
                for list in list_contacts:
                    print list
            def save(self):
                fa = file(file_contacts, "a")
                cPickle.dump(self.contacts, fa)
                fa.close() 
        i = class_contacts(list_contacts)
        i.append('joan', 'joan@123.com')
        i.append('adny', 'adny@123.com')
        i.append('xixi', 'xixi@123.com')
        i.find('joan')
        i.find('joab')
        print "Original List:"
        i.show()
        print "after modify adny"
        i.modify('adny', 'adnX@123.com')
        i.show()
        print "after del joan"
        i.delete('joan')
        i.show()
        i.save() 
    except TypeError:
        print "TypeError"
    except:
    print "Other Error occured" 
    

      

    服务项目 技术咨询 微信图书 微信视频 微信代码 定制开发 其他福利
    服务入口 QQ群有问必答
    查看详情
    一本书解决90%问题
    查看详情
    微信开发视频
    小程序开发视频
    免费代码
    ¥1888阿里云代金券
    查看详情
    营销工具
    微信特异功能
  • 相关阅读:
    INVALID_STATE_ERR: DOM Exception 11
    测试用户的网络环境
    CentOS修改IP、DNS、网关
    读《结网》
    命名函数表达式
    JavaScript中的编码函数
    linux下C程序printf没有立即输出的问题及我的Makefile文件
    学习使用vimperator
    thinkpad e40 安装 nvidia显卡驱动之后
    fedora:在命令行下删除文件到回收站
  • 原文地址:https://www.cnblogs.com/txw1958/p/2210084.html
Copyright © 2011-2022 走看看