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

  1. #!/usr/bin/python
  2. '''''
  3. Compiler:Python2.6
  4. Filename:tb.py
  5. Version:1.00@20081205
  6. Author:t0nsha
  7. (liaodunxia{at}gmail.com)
  8. Remark:Justasimpletelephonebookprogram
  9. formyoneweekpythonstudy.
  10. '''
  11. importcPickleasp
  12. tbDict={}
  13. tbdat='tb.dat'
  14. classPerson:
  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. deftbAdd(a):
  21. whileTrue:
  22. name=raw_input('EnteranameforAdd:')
  23. ifname=='Q':
  24. break
  25. iftbDict.has_key(name):
  26. print'%salreadyexists.'%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'%sadded,total:%d.'%(name,len(tbDict))
  37. ifa!='A':
  38. break
  39. deftbRemove(r):
  40. whileTrue:
  41. name=raw_input('EnteranameforDelete:')
  42. ifname=='Q':
  43. break
  44. ifnottbDict.has_key(name):
  45. print'%snotexist.'%name
  46. else:
  47. deltbDict[name]
  48. print'%sremoved.'%name
  49. ifr!='R':
  50. break
  51. deftbSearch(s):
  52. whileTrue:
  53. name=raw_input('EnteranameforSearch:')
  54. ifname=='Q':
  55. break
  56. iftbDict.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'%snotfound.'%name
  63. ifs!='S':
  64. break
  65. deftbList(l):
  66. ifl=='l':
  67. forkeyintbDict.keys():
  68. printkey
  69. elifl=='L':
  70. fornameintbDict.keys():
  71. print'name:'.rjust(12),tbDict[name].name
  72. print'phone:'.rjust(12),tbDict[name].phone
  73. print'email:'.rjust(12),tbDict[name].email
  74. print'address:'.rjust(12),tbDict[name].address
  75. print'total:%d.'%len(tbDict)
  76. deftbModify(m):
  77. whileTrue:
  78. name=raw_input('EnteranameforModify:')
  79. ifname=='Q':
  80. break
  81. ifnottbDict.has_key(name):
  82. print'%snotexist.'%name
  83. return
  84. phone=raw_input('newphone:')
  85. email=raw_input('newemail:')
  86. address=raw_input('newaddress:')
  87. person=Person(name=name,/
  88. phone=phone,/
  89. email=email,/
  90. address=address)
  91. tbDict[name]=person
  92. print'%smodified.'%(name)
  93. ifm!='M':
  94. break
  95. deftbUsage():
  96. print'''''
  97. Usage:
  98. a/A-->addanewperson/loopadd
  99. l/L-->listallnames/listalldetails
  100. r/S-->removeone/loopremove
  101. s/S-->search/loopsearch
  102. q/Q-->quit&save/quit(loop)butnotsave
  103. '''
  104. deftbQuit(q):
  105. ifq=='q':
  106. p.dump(tbDict,file(tbdat,'w+'))
  107. deftbLoop():
  108. whileTrue:
  109. c=raw_input('Readytowork:')
  110. ifc=='a'orc=='A':
  111. tbAdd(c)
  112. elifc=='l'orc=='L':
  113. tbList(c)
  114. elifc=='m'orc=='M':
  115. tbModify(c)
  116. elifc=='r'orc=='R':
  117. tbRemove(c)
  118. elifc=='s'orc=='S':
  119. tbSearch(c)
  120. elifc=='q'orc=='Q':
  121. tbQuit(c)
  122. break
  123. else:
  124. tbUsage()
  125. #Whilefirstrun,file"tb.dat"doesn'texist,this
  126. #willraiseanIOErrorexception,butweigoredit.
  127. try:
  128. tbDict=p.load(file(tbdat))
  129. exceptIOError:
  130. pass
  131. tbLoop()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值