Advertisement
Guest User

Untitled

a guest
Mar 20th, 2018
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. phonebook = dict()
  2. def Menu():
  3. print("\nWelcome to the phonenbook")
  4. print("----------------------------")
  5. print("1: delete a contact:")
  6. print("2: Search contact by name:")
  7. print("3: Display all contacts:")
  8. print("4: Add a contact:")
  9. print("-----------------------------")
  10. choice = input(str("choose from menu"))
  11. if choice == "4":
  12. ADD_to()
  13. elif choice =="3":
  14. Display()
  15.  
  16. elif choice == "2":
  17. search()
  18.  
  19.  
  20. elif choice =="1":
  21. Delete_con()
  22.  
  23. else:
  24. print("INVALID CHOICE")
  25.  
  26.  
  27. def ADD_to():
  28. name = input("enter a name to store")
  29. phonenumber = input("enter phone number to save")
  30. phonebook[name]=phonenumber
  31. with open("phonebook1.txt", "w+") as file:
  32. for keys,values in (phonebook.items()):
  33. display = file.writelines(keys + ":")
  34. display = file.writelines(values + "\n")
  35. return Menu()
  36.  
  37. def Display_dict():
  38. for items in phonebook:
  39. print(items)
  40.  
  41.  
  42. def Display():
  43. with open("phonebook1.txt", "r") as file:
  44. print(file.read())
  45.  
  46. def Delete_con():
  47. contact_to_delete= input("choose name to delete from contact")
  48.  
  49. with open("phonebook1.txt", "r") as file:
  50. content = file.readlines() #Read lines from text
  51. content = [line for line in content if line not in [contact_to_delete]] #Check if user input is in line
  52. with open("phonebook1.txt", "w") as file: #Write back content to text
  53. file.writelines(content)
  54.  
  55.  
  56. def Load_dictionary():
  57. with open('phonebook1.txt', 'r') as file:
  58. for line in file:
  59. x = line.split(":")
  60. a=x[0]
  61. b=x[1]
  62. phonebook[a]=b
  63.  
  64.  
  65. def search():
  66. search_this = input("Enter name to search in contact list:")
  67. if search_this in phonebook:
  68. print(" {} : {}".format(search_this,phonebook.get(search_this)))
  69.  
  70.  
  71. Load_dictionary()
  72. Menu()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement