Can someone review this and give it some tips? Ive awlways struggled with python programming.

# address book


    # Tyler G 


    def search_database ():
        #search algoritm
        searchcriteria = raw_input("What do you want to search? ")
        temp1 = open("addressbookdata","r")
        for line in temp1:
            if searchcriteria in line:
                print ("\n")

                print ("\n")
            else:
                print "Search yeilded zero results"
    def append_to_database (info):
        # append the info list to file
        temp1 = open("addressbookdata","a")
        temp1.write(info)
        temp1.write("\n")
        print ("The record was added to the database address book ")

    def collect ():
        # collect information...
        # append_to_file ([lastname, firstname]) # etc...
        lastname = raw_input("What is the persons last name? ")
        firstname = raw_input("What is the persons first name? ")
        phone = raw_input("What id the persons phone number? ")
        email = raw_input("What is the persons email address? ")
        address = raw_input("What is the persons address? ")
        info = (firstname + " " + lastname + ", " + phone + ", " + email + ", " + address)
        append_to_database (info)

    def menu ():
        answer = raw_input("Are You Creating An Entry [Press 1] \n Or Are You Searching An Entry [Press 2] ")

        # ask user what to do
        if answer == "1":
            collect ()
        if answer == "2":
            search_database()
        else:
            print str(answer) + " is not a valid option, try again"
            menu()

    # start everthing:
    menu ()

If they choose Use an address book:
First, ask the user for the name of the le in which their address book is stored (e.g., addresses.txt).
Import the information in that le, and save it in a dictionary; each line of the le should become
an entry in the dictionary. The key for each entry should be the contact's name. The value for
each entry should contain their address, phone number, and e-mail. (You can choose how to store
that information { the value could be a list, one long string, an object of a class you create, etc.)
Give the user options to: see an alphabetical list of every name in the address book; enter a person's
name and look up their contact info; or quit. Let them keep requesting information until they quit.
If they choose Create an address book:
Ask the user for the name of the le where the new address book should be stored.
Give the user three options: Create a single entry, Import another address book, or Quit.
To create a single entry, have them enter the relevant information one piece at a time.
To import another address book, ask them for the le name, then copy all the information in that
le to the new address book.
Allow the user to continue creating individual entries or importing other address books, writing all
the contact info to the le they named, until they choose to quit.
Throughout the program, attempt to make sure that the program will never crash fo

You put the space and data coma separated into the text file. So you have no hope of retrieving the fields reliably. You have to make some thoughts how to store and search the data (pickle, shelf, sqlite etc...)

Otherwise it is working.

Some ideas in the code:

import shelve
DTB=shelve.open("addressbookdata.db")

def search_database (searchcriteria ):
    #search algoritm
    for line in DTB.itervalues():
        if searchcriteria in line:
            print ("\n")
            print line
            print ("\n")
            break
    else:
        print "Search yeilded zero results"

def append_to_database (info):
    # append the info list to file
    DTB[str(hash(info))]=info
    print ("The record was added to the database address book ")

def collect ():
    # collect information...
    # append_to_file ([lastname, firstname]) # etc...
    lastname = raw_input("What is the persons last name? ")
    firstname = raw_input("What is the persons first name? ")
    phone = raw_input("What id the persons phone number? ")
    email = raw_input("What is the persons email address? ")
    address = raw_input("What is the persons address? ")
    info = (firstname + "," + lastname + ", " + phone + ", " + email + ", " + address)
    append_to_database (info)

def menu ():
    answer = raw_input("Are You Creating An Entry [Press 1] \n Or Are You Searching An Entry [Press 2] ")

    if answer == "1":
        collect()
    if answer == "2":
        searchcriteria = raw_input("What do you want to search? ")
        search_database(searchcriteria)
    else:
        print str(answer) + " is not a valid option, try again"
        menu()

menu ()

I'm struggling with searching the database. How should the notepad file look? It keeps telling me that print line is not a valid command.

Where should the address.txt file be in the computer? and how should it be setup?

def append_to_database (info):
        # append the info list to file
    temp1 = open("addresses.txt","a")
    temp1.write(info)
    temp1.write("\n")
    print ("The record was added to the database address book ")
def collect ():
        # collect information...
        # append_to_file ([lastname, firstname]) # etc...
    lastname = input("What is the persons last name? ")
    firstname = input("What is the persons first name? ")
    phone = input("What id the persons phone number? ")
    email = input("What is the persons email address? ")
    address = input("What is the persons address? ")
    info = (firstname + " " + lastname + ", " + phone + ", " + email + ", " + address)
    append_to_database (info)
def search_people():
    print("Alright, so here is where you would search for people already in the address book.")
    criteria = input("Please enter any keywords, including address, phone number, etc. for the search: ")
    temp1 = open("addresses.txt", "r")
    for line in temp1:
        if criteria in line:
            print(line)
        else:
            print("Did not find person.  Please add him/her/it to the address book first.")
    restart()
def browse_contacts():
    print("Alright, so this is where you will see a list of all your contacts, and all their information.")
    temp1 = open("addresses.txt", "r")
    readfile = temp1.read()
    print (readfile)
    restart()
def menu ():
    answer = input("Are You Creating An Entry [Press 1] \n Or Are You Searching An Entry [Press 2] \n Or are you browsing contacts? [Press 3] \n Or are you searching? [press 4]")
    if answer == "1":
        collect()
    if answer == "3":
        browse_contacts()
    if answer == "4":
        search_people() 
    else:
        print ("is not a valid option, try again")
        menu()

def restart():
    userinput = input("Would you like to restart the program to perform another operation? [y/n] ")
    if userinput == 'y':
        menu()
    elif userinput == 'n':
        print("Your addressbook will close.")
    else:
        print("Not a valid command.")
        restart()
menu()

Not most beautiful user interface, but it works for me. Of course, as I am using Python 2.7, I redefined input same as raw_input for it to work, by adding to beginning

try:
    input = raw_input
except:
    pass
This article has been dead for over six months. Start a new discussion instead.