Advertisement
Guest User

Untitled

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