Advertisement
Kimossab

Simple Contact System (testing)

Jul 8th, 2015
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.15 KB | None | 0 0
  1. '''
  2. IMPORTS
  3. '''
  4. import io
  5. import os.path
  6. import sys
  7. import ast
  8.  
  9. '''
  10. GLOBAL VARS
  11. '''
  12. #Using a list of dictionaries so I can addapt it to use more info other than name and number
  13. contact_list = [];
  14.  
  15. '''
  16. FUNCTIONS
  17. '''
  18. def LoadContact():
  19.     if not os.path.isfile("contacts.txt"): #If doesn't exist then return
  20.         return
  21.     fich = open("contacts.txt","r")
  22.     for line in fich:
  23.         contact_list.append(ast.literal_eval((line[:-1] if line[-1] == "\n" else line)))
  24.     fich.close();
  25.  
  26. def Menu():
  27.     os.system("cls")
  28.     print('''\
  29. MENU...
  30. 1-      Look for a contact.
  31. 2-      Insert a contact.
  32. 3-      List contacts by letter.
  33. 0-      Leave.
  34. ''')
  35.     return input("Option: ")
  36.  
  37. def SearchContact(Name):
  38.     Name = Name.upper()
  39.     for i in contact_list:
  40.         if i['Name'].upper() == Name:
  41.             return i
  42.     return 'NULL'
  43.  
  44. def SearchLetter(l):
  45.     l = l.upper()
  46.     print('Name             Number')
  47.     for i in contact_list:
  48.         if i['Name'][0].upper() == l[0]:
  49.             print("{}     {}".format(i['Name'],i['Number']))
  50.  
  51. def Save():
  52.     fich = open("contacts.txt","w")
  53.     for i in contact_list:
  54.         n = i['Name']
  55.         num = i['Number']
  56.         fich.write("{")
  57.         fich.write("'Name':'{}','Number':'{}'".format(n,num))
  58.         fich.write("}\n")
  59.     fich.close();
  60.  
  61. '''
  62. MAIN
  63. '''
  64. LoadContact();
  65. while True:
  66.     opc=int(Menu())
  67.     if opc == 0:
  68.         Save()
  69.         break
  70.     elif opc == 1:
  71.         con = SearchContact(input("\nWrite a name to look for: "))
  72.         if con == 'NULL':
  73.             print('Name not found.')
  74.         else:
  75.             print("Name: {}\nNumber: {}".format(con['Name'],con['Number']))
  76.         os.system("pause")
  77.     elif opc == 2:
  78.         n = {'Name':input('Type the name: '),'Number':input('Type the number: ')}
  79.         contact_list.append(n)
  80.         print('Contact ({}, {}) successfully inserted.'.format(n['Name'], n['Number']))
  81.         os.system("pause")
  82.     elif opc == 3:
  83.         SearchLetter(input('Type the leter you want to look for: '))
  84.         os.system("pause")
  85.     else:
  86.         print('Wrong option: Use only from 0 to 3.')
  87.         os.system("pause")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement