简易Python电话本(Simple Python Telephone Book)

  1. #!/usr/bin/python
  2. '''''
  3. Compiler: Python 2.6
  4. Filename: tb.py
  5. Version: 1.00 @20081205
  6. Author: t0nsha
  7.                 (liaodunxia{at}gmail.com)
  8. Remark: Just a simple telephone book program 
  9.                 for my one week python study. 
  10. '''
  11. import cPickle as p
  12. tbDict={}
  13. tbdat = 'tb.dat'
  14. class Person:
  15.     def __init__(self, name, phone, email, address):
  16.         self.name = name
  17.         self.phone = phone
  18.         self.email = email
  19.         self.address = address
  20. def tbAdd(a):
  21.     while True:
  22.         name = raw_input('Enter a name for Add:')
  23.         if name == 'Q':
  24.             break
  25.         if tbDict.has_key(name):
  26.             print '%s already exists.' % name
  27.             continue
  28.         phone = raw_input('phone:')
  29.         email = raw_input('email:')
  30.         address = raw_input('address:')
  31.         person = Person(name = name, /
  32.                         phone = phone, /
  33.                         email = email, /
  34.                         address = address)
  35.         tbDict[name] = person
  36.         print '%s added, total: %d.' % (name, len(tbDict))
  37.         if a != 'A':
  38.             break
  39. def tbRemove(r):
  40.     while True:
  41.         name = raw_input('Enter a name for Delete:')
  42.         if name == 'Q':
  43.             break
  44.         if not tbDict.has_key(name):
  45.             print '%s not exist.' % name
  46.         else:
  47.             del tbDict[name]
  48.             print '%s removed.' % name
  49.         if r != 'R':
  50.             break
  51. def tbSearch(s):
  52.     while True:
  53.         name = raw_input('Enter a name for Search:')
  54.         if name == 'Q':
  55.             break
  56.         if tbDict.has_key(name):
  57.             print 'name:'.rjust(12),tbDict[name].name
  58.             print 'phone:'.rjust(12),tbDict[name].phone
  59.             print 'email:'.rjust(12),tbDict[name].email
  60.             print 'address:'.rjust(12),tbDict[name].address
  61.         else:
  62.             print '%s not found.' % name
  63.         if s != 'S':
  64.             break
  65.     
  66. def tbList(l):
  67.     if l == 'l':
  68.         for key in tbDict.keys():
  69.             print key
  70.     elif l == 'L':
  71.         for name in tbDict.keys():
  72.             print 'name:'.rjust(12),tbDict[name].name
  73.             print 'phone:'.rjust(12),tbDict[name].phone
  74.             print 'email:'.rjust(12),tbDict[name].email
  75.             print 'address:'.rjust(12),tbDict[name].address
  76.     print 'total: %d.' % len(tbDict)
  77. def tbModify(m):
  78.     while True:
  79.         name = raw_input('Enter a name for Modify:')
  80.         if name == 'Q':
  81.             break
  82.         if not tbDict.has_key(name):
  83.             print '%s not exist.' % name
  84.             return
  85.         phone = raw_input('new phone:')
  86.         email = raw_input('new email:')
  87.         address = raw_input('new address:')
  88.         person = Person(name = name, /
  89.                         phone = phone, /
  90.                         email = email, /
  91.                         address = address)
  92.         tbDict[name] = person
  93.         print '%s modified.' % (name)
  94.         if m != 'M':
  95.             break
  96. def tbUsage():
  97.     print '''''
  98. Usage:
  99.     a/A --> add a new person/loop add
  100.     l/L --> list all names/list all details
  101.     r/S --> remove one/loop remove
  102.     s/S --> search/loop search
  103.     q/Q --> quit & save/quit (loop) but not save
  104. '''
  105. def tbQuit(q):
  106.     if q == 'q':
  107.         p.dump(tbDict, file(tbdat, 'w+'))
  108.         
  109. def tbLoop():
  110.     while True:
  111.         c = raw_input('Ready to work:')
  112.         if   c == 'a' or c == 'A':
  113.             tbAdd(c)
  114.         elif c == 'l' or c == 'L':
  115.             tbList(c)
  116.         elif c == 'm' or c == 'M':
  117.             tbModify(c)
  118.         elif c == 'r' or c == 'R':
  119.             tbRemove(c)
  120.         elif c == 's' or c == 'S':
  121.             tbSearch(c)
  122.         elif c == 'q' or c == 'Q':
  123.             tbQuit(c)
  124.             break
  125.         else:
  126.             tbUsage()
  127. #While first run, file "tb.dat" doesn't exist, this
  128. #will raise an IOError exception, but we igored it.
  129. try:
  130.     tbDict = p.load(file(tbdat))
  131. except IOError:
  132.     pass
  133. tbLoop()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值