python小练习

了解下python,看了觉得很简单,写一写发现还得练~

#!/usr/bin/python

import cPickle
import os

class ContactList:
    '''this is a contact list'''
    personList = {} #data container

    def __loadData(self):
        if (os.path.isfile('notebook') == False):
            tmp = file('notebook','w')
            tmp.close
            return
        f = file('notebook','r')
        self.personList = cPickle.load(f)
        f.close()
    def __init__(self,person):       
        self.personList.append(person)
    def __init__(self):
        pass
    def showList(self):
        self.__loadData()
        if (len(self.personList) == 0):
            print 'No data'
            return
        for name,tel in self.personList.items():
            print name+':'+tel
    def addPerson(self,name,tel):
        self.personList[name] = tel
        self.save()
    def delPerson(self,name):
        del self.personList[name]
        self.save()
    def modifyTel(self,name,number):
        self.personList[name] = tel
        self.save()
    def save(self):
        f = file('notebook','w')
        cPickle.dump(self.personList,f)
        f.close()

#the list
notebook = ContactList()
while True:
    userIn = raw_input('pls input your select: \ns(show) \na(add)\nd(delete)\nm(modify\ne(ecs)\ninput:')
    if (userIn == 'a'):
        name = raw_input('pls input name:')
        tel = raw_input('pls input tel number:')
        print 'Your input:person name %s tel number is %s' % (name,tel)
        notebook.addPerson(name,tel)
        notebook.save()
    elif (userIn == 's'):
        notebook.showList()
    elif (userIn == 'e'):
        print '-------------last list info-----------------'
        notebook.showList()
        print '-------------------------------------------'
        break;
    elif (userIn == 'd'):
        name = raw_input('Input the name you want to delete:')
        notebook.delPerson(name)
        print 'current person list:'
        notebook.showList()

    elif (userIn == 'm'):
        name = raw_input('Input the name want to update:')
        tel = raw_input('Input the new tel for '+name+':')
        notebook.modifyTel(name,tel)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值