Simple Python phone book)

Source: Internet
Author: 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 ():
  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 ':
  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: '. Must ust (12), tbdict [name]. Name
  58. Print 'phone: '. Empty ust (12), tbdict [name]. Phone
  59. Print 'email: '. Must ust (12), tbdict [name]. Email
  60. Print 'address: '. Must ust (12), tbdict [name]. Address
  61. Else:
  62. Print '% s not found.' % name
  63. If s! ='S ':
  64. Break
  65. Def tblist (l ):
  66. If l = 'l ':
  67. For key in tbdict. Keys ():
  68. Print key
  69. Elif L = 'l ':
  70. For name in tbdict. Keys ():
  71. Print 'name: '. Must ust (12), tbdict [name]. Name
  72. Print 'phone: '. Empty ust (12), tbdict [name]. Phone
  73. Print 'email: '. Must ust (12), tbdict [name]. Email
  74. Print 'address: '. Must ust (12), tbdict [name]. Address
  75. Print 'total: % d. '% Len (tbdict)
  76. Def tbmodify (m ):
  77. While true:
  78. Name = raw_input ('enter a name for modify :')
  79. If name = 'q ':
  80. Break
  81. If not tbdict. has_key (name ):
  82. Print '% s not exist.' % name
  83. Return
  84. Phone = raw_input ('new Phone :')
  85. Email = raw_input ('new Email :')
  86. Address = raw_input ('new address :')
  87. Person = person (name = Name ,/
  88. Phone = phone ,/
  89. Email = Email ,/
  90. Address = address)
  91. Tbdict [name] = person
  92. Print '% S Modified.' % (name)
  93. If M! = 'M ':
  94. Break
  95. Def tbusage ():
  96. Print '''''
  97. Usage:
  98. A/A --> Add a new person/loop add
  99. L/L --> list all names/list all details
  100. R/S --> remove one/loop remove
  101. S/S --> Search/loop search
  102. Q/Q --> quit & Save/quit (loop) but not save
  103. '''
  104. Def tbquit (q ):
  105. If q = 'q ':
  106. P. Dump (tbdict, file (tbdat, 'W + '))
  107. Def tbloop ():
  108. While true:
  109. C = raw_input ('ready to work :')
  110. If C = 'A' or C = 'A ':
  111. Tbadd (c)
  112. Elif c = 'L' or C = 'l ':
  113. Tblist (c)
  114. Elif c = 'M' or C = 'M ':
  115. Tbmodify (c)
  116. Elif c = 'R' or C = 'r ':
  117. Tbremove (c)
  118. Elif c ='s or C ='s ':
  119. Tbsearch (c)
  120. Elif c = 'q' or C = 'q ':
  121. Tbquit (c)
  122. Break
  123. Else:
  124. Tbusage ()
  125. # While first run, file "TB. dat" doesn't exist, this
  126. # Will raise an ioerror exception, but we igored it.
  127. Try:
  128. Tbdict = P. Load (file (tbdat ))
  129. Handle t ioerror:
  130. Pass
  131. Tbloop ()
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.