954,525 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Python Phone Book Trouble!!

Oh do I need some serious help! lol. I'm working on a phone book in python. For some reason when I select the option, enter....say....the last name, even though it's on the file that my program is reading it tells me that it's not there.
Can somebody please look at my program, and help point out where I'm going wrong or what I'm missing? I'm rather new at this, so be gentle! ;-)

def main():  #Set up contact lists.
    myList = []         
    
    option = displayMenu()
    while option != '4':   # Set up menu choices.
        if option == '1':
            getLastName(myList)
        elif option == '2':
            getFirstName(myList)
        elif option == '3':
            getPhoneNumber(myList)
          
        option = displayMenu()
        
    print "\nThank you for looking.\n\n"


def displayMenu(): # Display menu options/Data Validation.
    choice = '0'
    while choice != '1' and choice != '2' and choice != '3' \
        and choice != '4':
        print """\n\nPlease select an option:
                                1. Search by Last Name:
                                2. Search by First Name:
                                3. Search by Phone Number:
                                4. End search."""

        choice = raw_input("Enter option --> ")

# Data validation
        if choice != '1' and choice != '2' and choice != '3' \
        and choice != '4':
            print "Invalid option. Please select again."

        return choice

# Opening and reading file into a three arrays in a loop.
entries = "C:\Documents and Settings\Brenda\Desktop\
CSCD 110 Assignments\entries.txt"
fileInput = open("entries.txt", "r")
myList = fileInput.read()
count = 0

for myString in fileInput:
    myString = myString.strip()
    myString = mystring. lower()
    myNum = count % 3
    if myNum == 0:
        lastName.append(myString)
    elif myNum == 1:
        firstName.append(myString)
    elif myNum == 2:
        phoneNumber.append(myString)
    count = count + 1
fileInput.close()

# Set up menu choice for looking up last name.

def getLastName(lastList):
    lastName = raw_input("Last name to lookup --> ").strip() .lower()
    
    pointer = 0
    if lastName in lastList:
        while True: 
            try:
                pointer = lastName.index(lastName, pointer)
                print lastName[pointer].title(),
                pointer = pointer + 1
            except:
                break
    else:
       print "No entry found for " + name.title()

    
# Set up menu choice for looking up first name.

def getFirstName(firstList):
    firstName = raw_input("First name to lookup --> ").strip() .lower()

    pointer = 0
    if firstName in firstList:
        while True:
            try:
                pointer = firstName.index(firstName, pointer)
                print firstName[pointer].title(),
                pointer = pointer + 1
            except:
                break
    else:
        print "No entry found for " + name.title()

    
# Set up menu choice for looking up phone number.

def getPhoneNumber(phoneList):
    phoneNumber = raw_input("Phone number to lookup --> ").strip() .lower()

    pointer = 0
    if phoneNumber in phoneList:
        while True:
            try:
                pointer = phoneNumber.index(phoneNumber, pointer)
                print phoneNumber[pointer].title(),
                pointer = pointer + 1
            except:
                break
    else:
        print "No entry found for " + phoneNumber.title()
   

main()
idreu2go4it
Newbie Poster
6 posts since Feb 2010
Reputation Points: 10
Solved Threads: 0
 

myList is always empty. And what happens when someone chooses '5'?

## *************************************
myList = []
## *************************************
option = displayMenu()
while option != '4': # Set up menu choices.
    if option == '1':
        getLastName(myList)
    elif option == '2':
        getFirstName(myList)
    elif option == '3':
        getPhoneNumber(myList)

    option = displayMenu()

print "\nThank you for looking.\n\n"


You might also take a look at the Python Styleguide. Code is easier to read when we all use the same conventions. http://www.python.org/dev/peps/pep-0008/ Finally, click the [ code ] icon at the top and post your code in between the tags.

woooee
Nearly a Posting Maven
2,454 posts since Dec 2006
Reputation Points: 777
Solved Threads: 714
 

There are only 4 options on my menu. When they enter 5, or any other invalid entry, then the data validation loop kicks in and tells them to choose again.

myList is always empty. And what happens when someone chooses '5'?

## *************************************
myList = []
## *************************************
option = displayMenu()
while option != '4': # Set up menu choices.
    if option == '1':
        getLastName(myList)
    elif option == '2':
        getFirstName(myList)
    elif option == '3':
        getPhoneNumber(myList)

    option = displayMenu()

print "\nThank you for looking.\n\n"
You might also take a look at the Python Styleguide. Code is easier to read when we all use the same conventions. http://www.python.org/dev/peps/pep-0008/ Finally, click the [ code ] icon at the top and post your code in between the tags.
idreu2go4it
Newbie Poster
6 posts since Feb 2010
Reputation Points: 10
Solved Threads: 0
 

Sorry, I was too subtle. Generally, a "not a valid entry" message is printed when someone enters some other option. Also, you would generally have a message displayed for 4 = exit, but it is your menu and you can design it as you choose.

woooee
Nearly a Posting Maven
2,454 posts since Dec 2006
Reputation Points: 777
Solved Threads: 714
 

You also have the following block of code in the middle of the functions, but it is not a function. To start, you might want to design the file read and write. One function would read the file and return a list. Another function would write the updated list to the file. You can use an indicator string at the beginning of each record, so name might start with "NM", and address with "AD", etc. This would avoid the calcs to determine if it is the first second or third record. But if you want to keep it the way it is just read three records at a time.

# Opening and reading file into a three arrays in a loop.
entries = "C:\Documents and Settings\Brenda\Desktop\
CSCD 110 Assignments\entries.txt"
fileInput = open("entries.txt", "r")
myList = fileInput.read()
count = 0
 
for myString in fileInput:
    myString = myString.strip()
    myString = mystring. lower()
    myNum = count % 3
    if myNum == 0:
        lastName.append(myString)
    elif myNum == 1:
        firstName.append(myString)
    elif myNum == 2:
        phoneNumber.append(myString)
    count = count + 1
fileInput.close()


And finally, this piece of code reads the file twice. The
myList = fileInput.read()
statement reads the entire file into myList. The
for myString in fileInput:
statement reads one record at a time, except there are no records to read because the previous read statement positioned the file pointer at the end of the file, so just eliminate the first read statement as below.

fileInput = open("entries.txt", "r")
##myList = fileInput.read()   ## redundant
count = 0
 
for myString in fileInput:
woooee
Nearly a Posting Maven
2,454 posts since Dec 2006
Reputation Points: 777
Solved Threads: 714
 

Thanks for your help and suggestions. I have to go to class right now, but I'll try playing around with your ideas after class. take care. ;-)

Sorry, I was too subtle. Generally, a "not a valid entry" message is printed when someone enters some other option. Also, you would generally have a message displayed for 4 = exit, but it is your menu and you can design it as you choose.
idreu2go4it
Newbie Poster
6 posts since Feb 2010
Reputation Points: 10
Solved Threads: 0
 

Just a note.
"C:\Documents .
For windows use. "C:\\Documents or "C:/Documents

snippsat
Practically a Posting Shark
808 posts since Aug 2008
Reputation Points: 353
Solved Threads: 294
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You