Karastoyanov
Karastoyanov

Reputation: 11

String Data with Python

I am currently learning python and I started to write my first personal project. I created a very simple phone book which can get name and phone number and store it in a dict. But after I close the program the data is wiped out and it's not stored in some kind of DB or text file. I would like to add an additional feature where all the contacts are saved in text file (I assume a database will be quite hard for me at this point) and every time I open the program I access this text file with the previously saved contacts and I can apply some of the options like search, delete or show all contacts.

I am attaching my code, any additional comments would be appreciated.

contact_book = {}

while True: 
    user_choice = input("Select one of the following operations: \n [C] - Create a new contact \n [A] - Show all contacts" 
                        "\n [S] - Search for a contact \n [D] - Delete contact \n [X] - Exit \n").upper()
    
    if user_choice == "C":
        contact_name = input()
        contact_number = input()
        contact_book[contact_name] = contact_number
        
    if user_choice == "A": 
        for name, number in contact_book.items():
            print(f'Contact name -> {name}. Phone number -> {number}')
    
    if user_choice == "S":
        search_name = input("Enter a contact name: ")
        if search_name in contact_book:
            print(f'{search_name} has been found in contact list. {search_name} phone number is {contact_book.get(contact_name)}')
        else: 
            print(search_name, "is not found in the contact list.")
    
    if user_choice == "D":
        contact_to_delete = input("Enter the contact name you would like to delete: ")
        if contact_to_delete in contact_book:
            contact_book.pop(contact_to_delete)
            print(f"{contact_to_delete} was removed from the contact list.")
        else:
            print(f"{contact_to_delete} has not been found.")
    
    if user_choice == "X":
        break

Upvotes: 1

Views: 119

Answers (1)

user17083192
user17083192

Reputation:

I would recommend using MySQL but you said it would be difficult for you. You can use the concept of file handling. In this, you can store your data in a file and can use the data after wards. If you are not aware of this concept, I would say that your project has an awesome feature but you're doing it too early. First learn the concept of file handling and then give an attempt. Also, if you use file handling, deleting the contact is tough, it'll be quite lengthy. If you know it, here's your code:

def create():
    f=open("Contacts.txt","a")
    contact={}
    contact_name=input("Enter name: ")
    contact_no=input("Enter number: ")
    contact[contact_name]=contact_no
    f.write(contact)
    f.close()
def show_all():
    f=open("Contacts.txt","r")
    for i in f.readlines():
        print (i)
    f.close()
def search():
    f=open("Contacts.txt","r")
    name=input("Enter name: ")
    for i in f.readlines():
        for j,k in i.items():
            if j==name:
                print(j,k)
    f.close()

#main
while True:
    user_choice = input("Select one of the following operations: \n [C] - Create a new contact \n [A] - Show all contacts" 
                        "\n [S] - Search for a contact \n [X] - Exit \n").upper()
    if user_choice=="C":
        create()
    elif user_choice=="A":
        show_all()
    elif user_choice=="S":
        search()
    elif user_choice=="X":
        break
    else:
        print("Invalid input, try again")

Upvotes: 1

Related Questions