HELP! I've been working on this assignment for a while and can't get it to work. It's supposed to pull contact info from a text file, and then return it. Every time I try to run it , it returns "no entry found" no matter what.
HELP!

Declare constants
lastname = 1
firstname = 2
phonenumber = 3
QUIT = 0
choice = 7
#define main
def main():
    my_file = 'entries.txt'
    fileInput = open('entries.txt','r')
    lastname =[]
    firstname =[]
    phonenumber =[]
    count = 1
    for line in fileInput:
        line= line.strip()
        line = line.lower ()
        if count % 3 ==1:
            lastname.append(line)
        elif count % 3 ==2:
            firstname.append (line)
        else:
            phonenumber.append(line)
            count = count + 1


    print(lastname)
    print(firstname)
    print(phonenumber)
#define menu funtion
    def menu(): 
        print("1.) Lookup contact by last name")
        print("2.) Lookup contact by first name")
        print("3.) Look up contact by phone number")
        print("0.) Quit")
    menu()
    choice= int(input("Enter a menu option"))


    if choice == 1:
        def looklast (lastname, firstname, phonenumber):
            name = input("Enter the last name for look up: ")
            name = name.lower()
            name = name.strip

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

    elif choice == 2:
        def lookfirst(lastname, firstname, phonenumber):
            name= input("enter the first name for lookup:")
            name = name.lower()
            name = name.strip()
            pointer=0

            if name in firstname:
                while True:
                    try:
                        pointer = firstname.index (fname, pointer)
                        print(firstname[pointer].title())
                        print(lastname[pointer].title())
                        print(phonenumber[pointer].title())
                        pointer = pointer + 1
                    except:
                        break
            else:
                print ("No entry found")
        lookfirst(lastname, firstname, phonenumber)

    elif choice == 3:
        def phonelook(lastname, firstname, phonenumber):
            number = input("Enter the phone number for look up: ")
            pointer = 0

            if number in phonenumber:
                while True:
                    try:
                        pointer = number. index (number, pointer)
                        print(firstname [pointer].title())
                        print(lastname [pointer].title())
                        print (phonenumber [pointer].title())
                        pointer = pointer +1
                        phonelook(lastname, firstname, phonenumber)
                    except:
                        break
                else:
                    print("No entry found")
        phonelook(lastname ,firstname, phonenumber)
    elif choice == 0:
        print("You chose to quit")

    else:
        print("Error")
    fileInput.close()

main()

Recommended Answers

All 2 Replies

Before print('No entry found') you could add print(name) and print(firstname) to see why the if name in firstname returned false.

You don't increment "count" correctly in the file input (I would guess since we don't know the layout of the file). The printing of lastname, firstname, etc. should show that. Also, use one lookup function only. Past the list to use to the function. So to lookup lastname, pass the lastname list to the function and "last name". Then input "Enter the %s to look up" % lit_passed_to_func.

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.