簡易Python電話本(Simple Python Telephone Book)

來源:互聯網
上載者:User
  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()
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.