Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- phonebook = dict()
- def Menu():
- print("\nWelcome to the phonenbook")
- print("----------------------------")
- print("1: delete a contact:")
- print("2: Search contact by name:")
- print("3: Display all contacts:")
- print("4: Add a contact:")
- print("-----------------------------")
- choice = input(str("choose from menu"))
- if choice == "4":
- ADD_to()
- elif choice =="3":
- Display()
- elif choice == "2":
- search()
- elif choice =="1":
- Delete_con()
- else:
- print("INVALID CHOICE")
- def ADD_to():
- name = input("enter a name to store")
- phonenumber = input("enter phone number to save")
- phonebook[name]=phonenumber
- with open("phonebook1.txt", "w+") as file:
- for keys,values in (phonebook.items()):
- display = file.writelines(keys + ":")
- display = file.writelines(values + "\n")
- return Menu()
- def Display_dict():
- for items in phonebook:
- print(items)
- def Display():
- with open("phonebook1.txt", "r") as file:
- print(file.read())
- def Delete_con():
- contact_to_delete= input("choose name to delete from contact")
- with open("phonebook1.txt", "r") as file:
- content = file.readlines() #Read lines from text
- content = [line for line in content if line not in [contact_to_delete]] #Check if user input is in line
- with open("phonebook1.txt", "w") as file: #Write back content to text
- file.writelines(content)
- def Load_dictionary():
- with open('phonebook1.txt', 'r') as file:
- for line in file:
- x = line.split(":")
- a=x[0]
- b=x[1]
- phonebook[a]=b
- def search():
- search_this = input("Enter name to search in contact list:")
- if search_this in phonebook:
- print(" {} : {}".format(search_this,phonebook.get(search_this)))
- Load_dictionary()
- Menu()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement