I am creating a phonebook and i have some problems. I get everything to work except im having trouble deleting an entry and also when i add an entry im having trouble sorting it in alphabetical order. This is my code below if someone can help it would be much appreciated

while True:
    Mainscreen = raw_input("Welcome To The PhoneBook [Please press enter]")
    answer = raw_input("Are You Creating An Entry [Press 1] \nOr Are You Searching An Entry [Press 2] \nOr Are you deleting an entry [Press 3] \nPlease Choose an option:")

# IF USER IS CREATING 

    if answer == "1" : 
        print ("This is where we create an entry")
    # COLLECTS INFORMATION

        lastname = raw_input("What is the persons last name? ")
        firstname = raw_input("What is the persons first name? ")
        middlename = raw_input("What is the persons middlename? ")
        birthdate = raw_input("What is the person birthdate? ")
        phone = raw_input("What is the persons phone number? ")
        email = raw_input("What is the persons email address? ")
        address = raw_input("What is the persons address? ")
        grouping = raw_input("What is the persons grouping? ")
    
    #CREATING or ADDING TO Phonebook

        temp1 = open("F:/Desktop/phonebook.txt","a")
    
    #create string to print to file
    #print temp1
    #print (firstname + " " + lastname + ", " + phone + ", " + email + ", " + address) 

        temp1.write(firstname + ", " + middlename + ", " + lastname + ", " + phone + ", " + email + ", " + address + ", "  + ", " + birthdate + ", " + grouping)
        temp1.write("\n")

# SEARCHING FOR YOUR CRITERIA

    elif answer == "2" :
        print ("This is where we search for a contact ")
        searchcriteria = raw_input("Enter your search Criteria, name, phonenumber, birthdate, email, address, grouping etc: ")
        print searchcriteria
        temp1 = open("F:/Desktop/phonebook.txt","r")
        for line in temp1:
            if searchcriteria in line:
                print line

    elif answer == "3" :
        delete = raw_input("Which person do you want to remove from the phone book? ")
        temp1 = open("H:/profile/desktop/phonebook.txt","r+")
        print delete, 'has been removed from the phone book'                


# USER DID'NT PICK CREATE OR SEARCH FOR AN OPTION 

    else :
        print ("Incorrect Answer")
        break 
        exit()

Recommended Answers

All 3 Replies

Your not actually deleting anything from the file. You will need python to read the file, find the line to delete, remove it from the list, and save the new file.

As for sorting, check this out.
Also, here on Daniweb, there are sorting questions, and I think a snippit or two dealing with alphabetization. Just browse through the python sub-forum, or hit google.

You think u can help me out in sorting it im having alot of trouble

Show what you are trying not only teacher's template. You have got the code from code snippet's and that should be more than enough help for you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.