Advertisement
ngbmameman

Untitled

Sep 6th, 2011
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.41 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. import pickle
  5.  
  6.  
  7. d = {}
  8.  
  9.  
  10.  
  11.  
  12. def main():
  13.     #reads
  14.     pkl_file = open('info.pkl', 'rb')
  15.     d2 = pickle.load(pkl_file)
  16.     pkl_file.close()
  17.  
  18.     print d2
  19.  
  20.  
  21.     print "------------------"
  22.     print "* 1. Add         *"
  23.     print "* 2. Delete      *"
  24.     print "* 3. Search      *"
  25.     print "* 4. Show all    *"
  26.     print "------------------"
  27.    
  28.     useri = raw_input('enter choice: ')
  29.    
  30.     if useri == '1':
  31.         name = raw_input('Enter name: ')
  32.         number = raw_input('Enter number: ')
  33.        
  34.        
  35.         if name in d:
  36.             print "Name already in use."
  37.             main()
  38.         else:
  39.             d[name] = number
  40.         #writes
  41.         output = open('info.pkl', 'wb')
  42.         pickle.dump(d, output)
  43.         output.close()
  44.         main()
  45.            
  46.            
  47.     elif useri == '2':
  48.         delname = raw_input('enter name to delete: ')
  49.         if delname in d2:
  50.             del d[delname]
  51.             #writes
  52.             output = open('info.pkl', 'wb')
  53.             pickle.dump(d, output)
  54.             output.close()
  55.             main()
  56.         else:
  57.             print "Not in dictionary"
  58.             main()
  59.        
  60.     elif useri == '3':
  61.  
  62.         pername = raw_input('Enter name of person to search: ')
  63.         if pername in d2:
  64.             print "Number of %s is: %s" % (pername, d2[pername])
  65.             main()
  66.         else:
  67.             print "Not in dict"
  68.             main()
  69.            
  70.     elif useri == '4':
  71.  
  72.         for name, number in d2.iteritems():
  73.             print "%s: %s" % (name, number)
  74.         main()
  75.  
  76.        
  77.     else:
  78.         print "Choose an option"
  79.         main()
  80.  
  81.    
  82.     return 0
  83.  
  84.  
  85.  
  86.  
  87. if __name__ == '__main__':
  88.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement